【问题标题】:Play a sound on UINavigationBar back button tapped在 UINavigationBar 上点击后退按钮播放声音
【发布时间】:2011-12-21 03:12:36
【问题描述】:

如何仅在 UINavigationBar 中点击“返回”按钮时播放声音?

【问题讨论】:

    标签: iphone uinavigationbar


    【解决方案1】:

    为此,您需要用播放声音的代码替换通常的“返回”按钮:

    //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];
    }
    

    但请注意,它看起来不像通常的“后退”按钮。它将是矩形的。如果您看起来像通常的“后退”按钮,请将其实现为带有自定义图像的自定义按钮(就像通常的“后退”按钮)

    【讨论】:

    • 不能只覆盖popViewControllerAnimated吗?
    • 我试图覆盖通常的“后退”按钮 - 这是不可能的。要覆盖 popViewControllerAnimated,您需要覆盖导航控制器类以使其执行您需要的操作。我认为我的答案中的解决方案要容易得多
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-08-29
      • 2012-06-16
      • 1970-01-01
      • 2014-09-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多