1

I would like to display ABPersonViewController as a modal instead of pushing it on the navigation stack. I've got this working but to keep a done button present I've had to use an NSTimer to add the button every 0.25 seconds because the done button may be removed when the view appears and is always removed when the app enters the forground. This is a pretty lame hack so I'm wondering if anyone has a better idea :)

I made a subclass of ABPersonViewController that adds the done button and starts the timer on view did load and invalidates it when the view is deallocated.

Here is what my code looks like to show the modal:

- (IBAction)showContactModal:(id)sender{
    CNABPersonViewController *personViewController = [[CNABPersonViewController alloc] init];
    personViewController.displayedPerson = self.contact.record;
    personViewController.addressBook = [[CNAddressBookManager sharedManager] addressBook];
    personViewController.viewDelegate = self;
    personViewController.shouldShowLinkedPeople = YES;
    UINavigationController *navigationController =
    [[UINavigationController alloc] initWithRootViewController:personViewController];
    navigationController.modalPresentationStyle = UIModalPresentationFormSheet;     
    [self presentViewController:navigationController animated:YES completion:nil];
}
keegan3d
  • 10,357
  • 9
  • 53
  • 77
  • Where are you putting the Done button? The `ABPersonViewController` puts its own Edit button on `rightBarButtonItem`. In theory (I haven't tried) you should be able to put the Done button as the `leftBarButtonItem`. – rmaddy Nov 26 '12 at 23:21
  • I am not allowing editing which is why I'm putting the button on the right. I did try it on the left too but ran into similar issues. When the app returns to the foreground the button would be gone. Also if I did allow editing the done button on the left would be replaced with a cancel button. I overrode setEditing:animated: to readd the button when not editing but this method is not called if the user cancels the editing. – keegan3d Nov 27 '12 at 06:37

1 Answers1

0

I had success in doing it like this. Insert this line to add a button to the navigation bar:

    personViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithTitle:@"Test" style:UIBarButtonItemStylePlain target:self action:@selector(_yourAddressBookAction)];

If this does not solve your problem, please show us the code that you had the issue with.

Klaas
  • 22,394
  • 11
  • 96
  • 107