检测用户何时点击 UINavigationController 的左侧按钮
可能的重复:
找出用户是否按下了 uinavigationcontroller 中的后退按钮?< /a>
我想在用户点击 UINavigationController 的左侧按钮(“后退”按钮)时播放声音。
我怎样才能检测到这个?
Possible Duplicate:
Find out if user pressed the back button in uinavigationcontroller?
I want to play a sound when the user taps on the left-side button of a UINavigationController (the "back" button).
How can I detect this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
UINavigationController 有一个委托,每次视图控制器即将被推入视图(以及之后)时都会调用该委托。您可以通过调用以下方法使视图控制器成为委托(例如在 viewDidLoad 中):
然后在视图控制器中实现方法
navigationController:willShowViewController:animated:
。UINavigationController has a delegate that gets called every time a view controller is about to get pushed into view (and right after). You can make your view controller the delegate simply by calling this (e.g. in viewDidLoad):
Then implement the method
navigationController:willShowViewController:animated:
in your view controller.另一种简单的方法是在 viewWillDisappear() 中实现声音:
或者更强大的方法,以确保声音仅在按下后退按钮时播放(如果您有其他方式可以从导航堆栈中弹出视图)是子类化 UINavigationController 并为后退按钮放入自定义操作方法。 此页面为您提供了如何执行此操作的示例。
该示例基本上是 UINavigationController 的子类,并覆盖
按下后退按钮时调用的方法。
Another simple way is to implement your sound in the viewWillDisappear():
Or a more robust method, to ensure the sound only plays when the back button is pressed (in the case you have other ways the view may be popped from the navigation stack) is to subclass the UINavigationController and put in a custom action method for your back button. This page gives you an example of how to do this.
The example basically subclasses UINavigationController and over-rides the
method which is called when the back button is pressed.