iPhone 应用程序中的后退导航
有什么方法可以当我按下 iPhone 应用程序中的后退按钮时捕获该事件?现在,我正在使用一个带有名为“返回”的图像的按钮,并为其定义操作。
我的客户端需要默认后退按钮指向上一个屏幕,因此根据单击事件,我应该能够使其指向特定屏幕,以便显示该屏幕的标题。
简而言之,我需要后退按钮来显示我需要的标题,这可能吗?
提前致谢。
Is there any way I can catch the event when I press the back button in my iPhone application? Right now I am using a button with image named Back on it and defining the action for that.
My client requires default back button pointing to previous screen so based on click event I should be able make it point to particular screen so that title of that screen is shown.
In short i need back button to show the title i required, is it possible?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您处理
– navigationBar:didPopItem:
消息,UINavigationBarDelegate
可能是最接近检测按钮是否已按下的方法。当按下后退按钮或当您的代码从导航控制器堆栈本身弹出视图时,将调用此函数。如果您的代码手动弹出视图,那么设置一个标志来指示代码何时启动弹出应该很简单,因此您可以确定何时点击后退按钮
在 UINavigationBarDelegate 实现中创建一个布尔属性,例如
poppedInCode
在代码执行弹出并实现委托之前将其设置为 true ,如下所示:这比当前接受的答案有优势,因为它不需要 Apple 文档所说的组件子类化不应该子类化。它还保留了内置后退按钮的所有行为,而无需您重写它。
UINavigationBarDelegate
is probably the closest you can get to detecting whether the button has been pressed if you handle the– navigationBar:didPopItem:
message.This will be called either when the back button is pressed or when your code pops the view off the navigation controller stack itself. If your code is popping views manually then it should be trivial to set up a flag to indicate when your code initiated a pop and therefore you can determine when the back button was tapped
In your UINavigationBarDelegate implementation create a boolean property such as
poppedInCode
which you set to true just before your code performs a pop and implement the delegate like:This has the advantage over the currently accepted answer in that it doesn't require subclassing of components that Apple's documentation says that you shouldn't be subclassing. It also retains all the behaviour of the built-in back button without you having to rewrite it.
有两种方法可以做到这一点:
自己实现后退按钮并调用
UINavigationController
的- (UIViewController *)popViewControllerAnimated:(BOOL)animated
子类
UINavigationController
并实现- (UIViewController *)popViewControllerAnimated:(BOOL)animated
进行处理并将调用传递给super
。UINavigationBarDelegate
允许您在处理– navigationBar:didPopItem:
消息时检测按钮是否已被按下。There are two ways to do this:
Implement the back button yourself and call the
UINavigationController
's- (UIViewController *)popViewControllerAnimated:(BOOL)animated
Subclass
UINavigationController
and implement- (UIViewController *)popViewControllerAnimated:(BOOL)animated
to do your processing and pass on the call tosuper
.As suggested in another answer
UINavigationBarDelegate
allows you to detect whether the button has been pressed if you handle the– navigationBar:didPopItem:
message.您可以在
viewWillDisappear:
方法中执行代码,并在viewWillAppear
中放置标志 bBackClicked = true ,如果您从当前视图控制器推送任何其他控制器,请放置标志 bBackClicked = false 。You can do your code in
viewWillDisappear:
method, and put flag bBackClicked = true inviewWillAppear
, and if you push any other controller from current view controller, put flag bBackClicked = false.此方法比提供的其他方法更简单。此方法的优点之一是您不需要使用委托方法使代码不必要地复杂化。如果您已经有 UINavigationController 的委托,则此方法也更容易实现,因为它一次只能有一个委托引用。
This method is simpler than the others provided. One advantage to this method is that you don't need to unnecessarily complicate your code with delegate methods. This method is also easier to implement if you already have a delegate for the
UINavigationController
, since it can only have a single delegate reference at a time.