2010-05-16 55 views
1

我怎樣才能彈出後退按鈕按下,我通過從以前的視圖控制器的下面的代碼。謝謝popViewControllerAnimated從後退按鈕

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

回答

3

沒有必要手動添加後退按鈕。

但是,如果您確實需要該自定義按鈕來彈出視圖控制器,則可以創建自定義消息。

-(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