Reputation: 65
I would like to remove the cancel button in the navigation bar of ABPeoplePickerNavigationController because I want to have an add button. I customized the navigation controller delegate in this way:
- (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;
}
}
It works fine in iOs 3.x and iOs 5.x, but in iOS 4.x I always have the cancel button in the navigation bar when the contacts list appears but if I select a contact then I go back to the first view controller the add button will appear.
How can explain this strange behavior only in iOS 4.x ?
Upvotes: 1
Views: 2326
Reputation: 3409
[picker setAllowsCancel:NO];
//picker is an object of ABPeoplePickerNavigationController.
Upvotes: 0
Reputation: 65
I found the solution:
-(void)viewDidAppear:(BOOL)animated {
NSLog(@"Contacts view did appear");
[super viewDidLoad];
picker = [[ABPeoplePickerNavigationController alloc]init];
[picker setDelegate:self];
[picker setAllowsCancel:NO];
self.picker.navigationBar.tintColor = [UIColor blackColor];
[self presentModalViewController:picker animated:YES];
}
So if you want to remove the cancel button in iOs 4.x you have to add this line: [picker setAllowsCancel:NO]; I received a warning from the compiler, but now the cancel button is removed in iOS 4.x
Upvotes: 1