我有一个启动一个UIViewController一个UITableViewController,每当后退按钮在子控制器,这是从"的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");
}
Run Code Online (Sandbox Code Playgroud)
Zor*_*mic 19
您可以实现viewWillDisappear
UIViewController 的方法.当你的控制器即将离开时,这会被调用(因为另一个被推到导航控制器堆栈上,或者因为按下了'后退'按钮).
要确定视图是否因按下后退按钮而消失,您可以使用在将新控制器推入导航控制器的任何位置设置的自定义标志,如下所示
- (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
}
}
Run Code Online (Sandbox Code Playgroud)
无论你在哪里推新的视图控制器,你都必须记住也设置那个标志......
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
...
viewPushed = YES;
[self.navigationController pushViewController:myNewController animated:YES];
...
}
Run Code Online (Sandbox Code Playgroud)
And*_*rew 17
自从被问到这个问题已经有一段时间了,但我只是试着自己这样做.我使用了类似于Zoran的解决方案,但是没有使用标志我做了这个:
- (void)viewWillDisappear: (BOOL)animated
{
[super viewWillDisappear: animated];
if (![[self.navigationController viewControllers] containsObject: self])
{
// the view has been removed from the navigation stack, back is probably the cause
// this will be slow with a large stack however.
}
}
Run Code Online (Sandbox Code Playgroud)
我认为它绕过了旗帜的问题,IMO更清洁,但效率不高(如果导航控制器上有很多项目).
Bla*_*ank 13
在我看来,最好的解决方案.
- (void)didMoveToParentViewController:(UIViewController *)parent
{
if (![parent isEqual:self.parentViewController]) {
NSLog(@"Back pressed");
}
}
Run Code Online (Sandbox Code Playgroud)
但它只适用于iOS5 +
我用这个代码:
- (void) viewWillDisappear:(BOOL)animated {
if ([self.navigationController.viewControllers indexOfObject:self] == NSNotFound)
{
// your view controller already out of the stack, it meens user pressed Back button
}
}
Run Code Online (Sandbox Code Playgroud)
但是当用户按下标签栏按钮并一步弹出到根视图控制器时,这不是实际的.对于这种情况使用此:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(viewControllerChange:)
name:@"UINavigationControllerWillShowViewControllerNotification"
object:self.navigationController];
- (void) viewControllerChange:(NSNotification*)notification {
NSDictionary* userInfo = [notification userInfo];
if ([[userInfo objectForKey:@"UINavigationControllerNextVisibleViewController"] isKindOfClass:[<YourRootControllerClass> class]])
{
// do your staff here
}
}
Run Code Online (Sandbox Code Playgroud)
别忘了:
[[NSNotificationCenter defaultCenter] removeObserver:self
name:@"UINavigationControllerWillShowViewControllerNotification"
object:self.navigationController];
Run Code Online (Sandbox Code Playgroud)
您可以制作自己的按钮并将其放置为leftBarButtonItem
. 然后让它调用你的方法,你可以做任何事情,并调用[self.navigationController popViewController...
你自己
归档时间: |
|
查看次数: |
27282 次 |
最近记录: |