gpt4 book ai didi

ios - 如何捕获后退按钮事件

转载 作者:IT王子 更新时间:2023-10-29 08:13:09 29 4
gpt4 key购买 nike

我有一个启动 UIViewController 的 UITableViewController,我想在子 Controller 中按下后退按钮时捕获,这是从“UIViewController”派生的类。我可以更改后退按钮标题,但在设置 backBarButtonItem 时设置目标和操作值似乎被忽略了。有什么方法可以接收某种类型的“后退”按钮被点击的通知?

- (void)showDetailView 
{
// How I'm creating & showing the detail controller
MyViewController *controller = [[MyViewController alloc] initWithNibName:@"MyDetailView" bundle:nil];

UIBarButtonItem *backButton = [[UIBarButtonItem alloc] initWithTitle:@"Pages"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(handleBack:)];

self.navigationItem.backBarButtonItem = backButton;
[backButton release];

[self.navigationController pushViewController:controller animated:animated];
[controller release];

}

- (void)handleBack:(id)sender
{
// not reaching here
NSLog(@"handleBack event reached");
}

最佳答案

您可以实现 UIViewController 的 viewWillDisappear 方法。当您的 Controller 即将离开时会调用此方法(因为另一个 Controller 被推到导航 Controller 堆栈上,或者因为按下了“后退”按钮)。

要确定 View 是否因为按下后退按钮而消失,您可以使用在将新 Controller 推到导航 Controller 的任何地方设置的自定义标志,如下所示

- (void)viewWillDisappear:(BOOL)animated {
[super viewWillDisappear:animated];
if (viewPushed) {
viewPushed = NO; // Flag indicates that view disappeared because we pushed another controller onto the navigation controller, we acknowledge it here
} else {
// Here, you know that back button was pressed
}
}

无论你在哪里推送一个新的 View Controller ,你都必须记住也要设置那个标志......

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
viewPushed = YES;
[self.navigationController pushViewController:myNewController animated:YES];
...
}

关于ios - 如何捕获后退按钮事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1557290/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com