沒有必要手動添加後退按鈕。
但是,如果您確實需要該自定義按鈕來彈出視圖控制器,則可以創建自定義消息。
-(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)];
我認爲'target' S和'action' S於這些會用來作爲'backBarButtonItem'被忽略了......我沒有測試過一個'UIBarButtonItem',但如果是這樣的話,你的代碼,以及任何其他代碼試圖覆蓋後退按鈕,將無法正常工作。 (只有'title'用於'backBarButtonItem' afaik。) – 2010-05-16 14:10:19
@DouweM:對啊。然後使用'leftBarButtonItem'。無論如何,我沒有看到重寫默認後退按鈕的理由。 – kennytm 2010-05-16 14:12:55
不,我也不是,但那是OP和你的代碼試圖做的;) – 2010-05-16 14:19:24