删除ABPeoplePickerNavigationController中的取消按钮。 iOs 4.x中的奇怪行为

时间:2012-02-02 16:44:22

标签: iphone ios4 uinavigationcontroller uibarbuttonitem abpeoplepickerview

我想删除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中解释这种奇怪的行为?

2 个答案:

答案 0 :(得分:1)

我找到了解决方案:

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

}

因此,如果要删除iOs 4.x中的取消按钮,则必须添加以下行: [picker setAllowsCancel:NO]; 我收到了编译器的警告,但现在在iOS 4.x中删除了取消按钮

答案 1 :(得分:0)

[picker setAllowsCancel:NO];

// picker是ABPeoplePickerNavigationController的一个对象。