iPhone 应用程序中的后退导航

发布于 2024-11-19 02:24:57 字数 207 浏览 2 评论 0原文

有什么方法可以当我按下 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

仅此而已 2024-11-26 02:24:57

如果您处理 – navigationBar:didPopItem: 消息,UINavigationBarDelegate 可能是最接近检测按钮是否已按下的方法。

当按下后退按钮或当您的代码从导航控制器堆栈本身弹出视图时,将调用此函数。如果您的代码手动弹出视图,那么设置一个标志来指示代码何时启动弹出应该很简单,因此您可以确定何时点击后退按钮

在 UINavigationBarDelegate 实现中创建一个布尔属性,例如 poppedInCode 在代码执行弹出并实现委托之前将其设置为 true ,如下所示:

- (void) navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
    if (!self.poppedInCode) {
        // back button was tapped
    }

    // set to false ready for the next pop
    self.poppedInCode = FALSE;
}

这比当前接受的答案有优势,因为它不需要 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:

- (void) navigationBar:(UINavigationBar *)navigationBar didPopItem:(UINavigationItem *)item
{
    if (!self.poppedInCode) {
        // back button was tapped
    }

    // set to false ready for the next pop
    self.poppedInCode = FALSE;
}

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.

安人多梦 2024-11-26 02:24:57

有两种方法可以做到这一点:

  1. 自己实现后退按钮并调用 UINavigationController- (UIViewController *)popViewControllerAnimated:(BOOL)animated

  2. 子类UINavigationController 并实现 - (UIViewController *)popViewControllerAnimated:(BOOL)animated 进行处理并将调用传递给 super

  3. 另一个答案中所建议的,UINavigationBarDelegate 允许您在处理 – navigationBar:didPopItem: 消息时检测按钮是否已被按下。

There are two ways to do this:

  1. Implement the back button yourself and call the UINavigationController's - (UIViewController *)popViewControllerAnimated:(BOOL)animated

  2. Subclass UINavigationController and implement - (UIViewController *)popViewControllerAnimated:(BOOL)animated to do your processing and pass on the call to super.

  3. As suggested in another answer UINavigationBarDelegate allows you to detect whether the button has been pressed if you handle the – navigationBar:didPopItem: message.

骄兵必败 2024-11-26 02:24:57

您可以在 viewWillDisappear: 方法中执行代码,并在 viewWillAppear 中放置标志 bBackClicked = true ,如果您从当前视图控制器推送任何其他控制器,请放置标志 bBackClicked = false 。

You can do your code in viewWillDisappear: method, and put flag bBackClicked = true in viewWillAppear, and if you push any other controller from current view controller, put flag bBackClicked = false.

坦然微笑 2024-11-26 02:24:57

此方法比提供的其他方法更简单。此方法的优点之一是您不需要使用委托方法使代码不必要地复杂化。如果您已经有 UINavigationController 的委托,则此方法也更容易实现,因为它一次只能有一个委托引用。

-(void)viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
        // 'Back' button was pressed.  We know this is true because self is no longer
        // in the navigation stack.
    }

    [super viewWillDisappear:animated];
}

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.

-(void)viewWillDisappear:(BOOL)animated {
    if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound) {
        // 'Back' button was pressed.  We know this is true because self is no longer
        // in the navigation stack.
    }

    [super viewWillDisappear:animated];
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文