ABPeoplePickerNavigationController - remove “Cancel” button without using private methods/properties?

二次信任 提交于 2019-12-05 10:05:46
user454083

this one

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
  UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(0.0f,0.0f,0.0f,0.0f)]; 
  UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom]; 
  //UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addAction)]; 
  [viewController.navigationItem setRightBarButtonItem:btn animated:NO]; 
  [btn release]; 
  [custom release]; 
}

works perfect!

The examples herein using the delegate method navigationController:willShowViewController:animated: do work, but it may be the case that you want to add your own navigation item in your own controllers and the given options will remove anything that you might set in your own controllers. Here's code that I've successfully used to get this option to work well:

- (void)navigationController:(UINavigationController *)navigationController
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated {

    // Here we want to remove the 'Cancel' button, but only if we're showing
    // either of the ABPeoplePickerNavigationController's top two controllers
    if ([navigationController.viewControllers indexOfObject:viewController] <= 1) {

        viewController.navigationItem.rightBarButtonItem = nil;
    }
}

Note that there are two view controllers in the navigation controller's stack, the one for contact groups and one for the contact list. This is why we cannot just check fi the viewController is the navigation controller's top view controller.

There is no answer to this - write a new person picker if you can't live with the cancel.

You can achieve that result browsing through the picker subviews. Just a little boring...

I haven't tried it yet, but I think Uby is saying to iterate through the subviews of the picker until you find one that is isKindOfClass:[UIBarButtonItem class] and then you can change it's title property. It might also be in the navigationBar's 'Item' array.

RaviYadav

Set delegate to PeoplePickerController controller. In the delegate class, have this delegate method.

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated
{
 UIView *pCustomView = [[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)];
 UIBarButtonItem *pBtn = [[UIBarButtonItem alloc] initWithCustomView:pCustomView];
 [viewController.navigationItem setRightBarButtonItem:pBtn animated:NO];
 [pBtn release];
 [pCustomView release];
}
valvoline

Be sure to set the delegate for picker object (not the peoplePickerDelegate, just the delegate) to the class that implement the following method:

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
UIView *custom = [[UIView alloc] initWithFrame:CGRectMake(0,0,0,0)];
UIBarButtonItem *btn = [[UIBarButtonItem alloc] initWithCustomView:custom];
[viewController.navigationItem setRightBarButtonItem:btn animated:NO];
[btn release];
[custom release];
} 
Michal

It works fine but in iOS 4 there is one more thing. When I switch back to my app using Fast App Switching feature, the cancel button appears again.

The method

- (void)navigationController:(UINavigationController *)navigationController  
      willShowViewController:(UIViewController *)viewController
                    animated:(BOOL)animated

doesn't get called. So I made this:

- (void)applicationDidEnterBackground:(UIApplication *)application {
    id topView = pickerControllerDelegate.peoplePicker.topViewController;
    topView.navigationItem.rightBarButtonItem = nil;
}

It works pretty well.

ansix

according to russel b you could just overwrite your viewdidapper

this worked for me:

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    UINavigationItem *item = (UINavigationItem *)[self.navigationBar.items lastObject];
    item.rightBarButtonItems = [[NSArray alloc] init];

    item.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
}

EDIT: See comments below. This is now an illustration of what not to do.

I tried to get the desired behavior with the public API by subclassing ABPeoplePickerNavigationController and intercepting all the events that change the current navigation view controller view. Then one can just navigate the view hierarchy and purge all the unwanted buttons.

You can navigate the view hierarchy from a delegate, but you aren't privy to the events that change the view state... which makes it hard to kill the Cancel button and make it stick.

This code kind of worked for me (NOTE: it brute-force kills all the right-hand buttons):

- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self killCancelButton];
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    [super pushViewController:viewController animated:animated];
    [self killCancelButton];
}

- (UIViewController*)popViewControllerAnimated:(BOOL)animated {
    UIViewController *result = [super popViewControllerAnimated:animated];
    [self killCancelButton];
    return result;
}

- (void)killCancelButton {
    for (NSUInteger itemIdx = 0; itemIdx < self.navigationBar.items.count; itemIdx++) {
        UINavigationItem *item = [self.navigationBar.items objectAtIndex:itemIdx];
        item.rightBarButtonItems = [[NSArray alloc] init];
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!