我想删除 ABPeoplePickerNavigationController 导航栏中的取消按钮,因为我想要一个添加按钮。我以这种方式自定义了导航控制器委托:
- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated{
switch ([navigationController.viewControllers count]) {
case 0: {
viewController.navigationItem.rightBarButtonItem = nil;
break;
}
case 1: {
viewController.navigationItem.rightBarButtonItem = nil;
break;
}
case 2: {
UIBarButtonItem *addButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson:)];
[viewController.navigationItem setRightBarButtonItem:addButtonItem animated:NO];
[addButtonItem release];
UIBarButtonItem *cancelButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancel:)];
[viewController.navigationItem setLeftBarButtonItem:cancelButtonItem animated:NO];
[cancelButtonItem release];
NSLog(@"View 2 %@",viewController.navigationItem.rightBarButtonItem);
break;
}
case 3: {
UIBarButtonItem *editButtonItem;
if ([viewController isKindOfClass:[ABPersonViewController class]]) {
editButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(editPerson:)];
self.personView = (ABPersonViewController*) viewController;
self.personView.allowsEditing = YES;
self.personView.personViewDelegate = self;
[viewController.navigationItem setRightBarButtonItem:editButtonItem animated:NO];
[editButtonItem release];
} else {
// ABPersonNewViewController
//No need to add codes here
}
break;
}
default: {
break;
}
}
它在 iOs 3.x 和 iOs 5.x 中运行良好,但在 iOS 4.x 中,当联系人列表出现时,导航栏中总是有取消按钮,但如果我选择一个联系人,那么我会返回第一个视图控制器添加按钮将出现。
如何仅在 iOS 4.x 中解释这种奇怪的行为?