AbpeoplePickernavigationController - Remova o botão "Cancelar" sem usar métodos/propriedades privadas?

StackOverflow https://stackoverflow.com/questions/1611499

Pergunta

Estou usando o ABPEOPLEPickernAvigationController, uma subclasse do UinavigationController e, no contexto, estou usando o botão de barra de navegação padrão para o lado direito "Cancelar", não faz sentido. Não consigo encontrar uma maneira de desativá-lo ou escondê-lo, e qualquer método usado para ser público e aprovável por lojas. Livrar -se completamente da barra NAV (Picker.NavigationBarhidden = Yes;) pode ser uma opção, exceto que, depois de voltar à lista de contatos, a barra NAV reaparece. Subclasse abpeoplePickernAvigationController e interceptar o ViewWillappear para tentar o botão cancelar não funcionou. [Picker setAllowsCancel: não]; Funciona, mas não está documentado, então espero que nunca aprova a aprovação.

Foi útil?

Solução 3

Não há resposta para isso - escreva um recipiente de uma nova pessoa se você não puder viver com o cancelamento.

Outras dicas

Este

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

Funciona perfeita!

Os exemplos aqui usando o Método Delegado Método Controlador: WillSowViewController: Animado: faça funcionar, mas pode ser o caso de que você deseja adicionar seu próprio item de navegação em seus próprios controladores e as opções fornecidas removerão qualquer coisa que você possa definir em seus próprios controladores . Aqui está o código que eu usei com sucesso para fazer com que essa opção funcione bem:

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

Observe que existem dois controladores de exibição na pilha do controlador de navegação, a para grupos de contato e um para a lista de contatos. É por isso que não podemos simplesmente verificar o FI que o ViewController é o controlador de exibição superior do controlador de navegação.

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.

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

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

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.

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];
    }
}
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top