在UINavigationBar后面按下按钮播放声音

时间:2011-12-21 03:12:36

标签: iphone uinavigationbar

如果在UINavigationBar中点击“后退”按钮,我该如何播放声音?

2 个答案:

答案 0 :(得分:2)

为此,您需要使用新的替换常用的“后退”按钮,并使用您的播放声音代码:

//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图像的自定义按钮(如通常的“后退”按钮)

答案 1 :(得分:1)

在你的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];
}