正确的方法为UIAlertView添加声音

向UIAlertView添加声音的最正确方法是什么?

我应该将其子类化,然后在视图出现时播放系统声音吗?

或者它是否更正确,不是为了子类而是调用一个单独的方法来播放声音?

任何人都可以推荐一个播放声音,看起来比UIAlertView漂亮的控件吗?

你可以通过使用AVAudioPlayer下面的代码实现这一点: –

在.h文件中设置UIAlertView dalagete,如: –

 @interface ViewController : UIViewController  

在.m文件中

 - (void)viewDidLoad { [super viewDidLoad]; NSURL *url = [NSURL fileURLWithPath:[NSString stringWithFormat:@"%@/foo.mp3", [[NSBundle mainBundle] resourcePath]]]; NSError *error; UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Title" message:@"message" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil]; audioPlayer = [[AVAudioPlayer alloc] initWithContentsOfURL:url error:&error]; audioPlayer.numberOfLoops = 1; [audioPlayer play]; [alert show]; } - (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==0) { [audioPlayer stop]; } } 

编辑

注意: –不要忘记添加AVAudio framework如果你想播放声音直到没有按下UIAlertView按钮然后设置audioPlayer.numberOfLoops = -1;