Iphone 点击UINavigationBar back按钮播放声音
如何仅在UINavigationBar中点击“后退”按钮时播放声音?要执行此操作,您需要使用播放声音的代码将常用的“后退”按钮替换为新按钮:Iphone 点击UINavigationBar back按钮播放声音,iphone,uinavigationbar,Iphone,Uinavigationbar,如何仅在UINavigationBar中点击“后退”按钮时播放声音?要执行此操作,您需要使用播放声音的代码将常用的“后退”按钮替换为新按钮: //in ViewDidLoad: UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(pop:)]; self
//in ViewDidLoad:
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(pop:)];
self.navigationItem.leftBarButtonItem = barBtnItem;
[barBtnItem release];
//action:
- (IBAction)pop:(id)sender {
//your code of playing sound here
[self.navigationController popViewControllerAnimated:YES];
}
但请注意,它看起来不像通常的“后退”按钮。它将是矩形的。如果您希望它看起来像普通的“后退”按钮,请将其像带有cusom图像的自定义按钮一样实现(像普通的“后退”按钮)要执行此操作,您需要用新按钮替换普通的“后退”按钮,并使用播放声音的代码:
//in ViewDidLoad:
UIBarButtonItem *barBtnItem = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(pop:)];
self.navigationItem.leftBarButtonItem = barBtnItem;
[barBtnItem release];
//action:
- (IBAction)pop:(id)sender {
//your code of playing sound here
[self.navigationController popViewControllerAnimated:YES];
}
但请注意,它看起来不像通常的“后退”按钮。它将是矩形的。如果您想让它看起来像普通的“后退”按钮,请将其像带有cusom图像的自定义按钮一样实现(像普通的“后退”按钮)在您的pop方法中,您可以通过以下方式播放声音:
- (IBAction)pop:(id)sender
{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@”soundName” ofType:@”wav”];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
[soundPath release];
[self.navigationController popViewControllerAnimated:YES];
}
在pop方法中,您可以通过以下方式播放声音:
- (IBAction)pop:(id)sender
{
NSString *soundPath = [[NSBundle mainBundle] pathForResource:@”soundName” ofType:@”wav”];
SystemSoundID soundID;
AudioServicesCreateSystemSoundID((CFURLRef)[NSURL fileURLWithPath: soundPath], &soundID);
AudioServicesPlaySystemSound (soundID);
[soundPath release];
[self.navigationController popViewControllerAnimated:YES];
}
不可能只覆盖popViewControllerAnimated吗?我试图覆盖通常的“后退”按钮-这是不可能的。若要重写popViewControllerAnimated,您需要重写导航控制器类,使其执行所需的操作。我认为我的答案中的解决方案更容易。是否不可能只覆盖popViewControllerAnimated?我试图覆盖通常的“后退”按钮-这是不可能的。若要重写popViewControllerAnimated,您需要重写导航控制器类,使其执行所需的操作。我认为我的答案中的解决方法要简单得多