popViewControllerAnimated从后退按钮

问题描述:

我怎样才能弹出后退按钮按下,我通过从以前的视图控制器的下面的代码。谢谢popViewControllerAnimated从后退按钮

self.navigationItem.backBarButtonItem = [[UIBarButtonItem alloc] 
     initWithTitle:@"Back" 
     style:UIBarButtonItemStyleBordered 
     target:nil action:nil]; 

没有必要手动添加后退按钮。

但是,如果您确实需要该自定义按钮来弹出视图控制器,则可以创建自定义消息。

-(void)popViewControllerWithAnimation { 
    [self.navigationController popViewControllerAnimated:YES]; 
} 
... 
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
    initWithTitle:@"Back" 
    style:UIBarButtonItemStyleBordered 
    target:self action:@selector(popViewControllerWithAnimation)]; 

或者创建一个NSInvocation。

NSInvocation* invoc = [NSInvocation invocationWithMethodSignature: 
     [self.navigationController methodSignatureForSelector:@selector(popViewControllerAnimated:)]]; 
// watch out for memory management issues: you may need to -retain invoc. 
[invoc setTarget:self.navigationController]; 
[invoc setSelector:@selector(popViewControllerAnimated:)]; 
BOOL yes = YES; 
[invoc setArgument:&yes atIndex:2]; 
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
    initWithTitle:@"Back" 
    style:UIBarButtonItemStyleBordered 
    target:invoc action:@selector(invoke)]; 
+0

我认为'target' S和'action' S于这些会用来作为'backBarButtonItem'被忽略了......我没有测试过一个'UIBarButtonItem',但如果是这样的话,你的代码,以及任何其他代码试图覆盖后退按钮,将无法正常工作。 (只有'title'用于'backBarButtonItem' afaik。) – 2010-05-16 14:10:19

+0

@DouweM:对啊。然后使用'leftBarButtonItem'。无论如何,我没有看到重写默认后退按钮的理由。 – kennytm 2010-05-16 14:12:55

+0

不,我也不是,但那是OP和你的代码试图做的;) – 2010-05-16 14:19:24