iosuinavigationcontrolleruinavigationbarabaddressbookaddressbookui

Can't back out of ABUnknownPersonViewController when adding person


I'm having an issue. ABUnknownPersonViewController is embedded in a navigation controller at the top view, however, I'm not using the navigation bar in my app. When pushing to the ABUnknownPersonView, I don't get the navigation bar or the back button. My solution was this:

ABUnknownPersonViewController *unknownPersonViewController = [[ABUnknownPersonViewController alloc] init];
unknownPersonViewController.displayedPerson = (ABRecordRef)[self buildContactDetails];
unknownPersonViewController.allowsAddingToAddressBook = YES;
unknownPersonViewController.unknownPersonViewDelegate = self;

UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, 41)]; navBar.delegate = self;

UINavigationItem *backItem = [[UINavigationItem alloc] initWithTitle:@"Back"];
[navBar pushNavigationItem:backItem animated:NO];

UINavigationItem *topItem = [[UINavigationItem alloc] initWithTitle:@"Your Title"];
[navBar pushNavigationItem:topItem animated:NO];
topItem.leftBarButtonItem = nil;

[unknownPersonViewController.view addSubview:navBar];
[self.navigationController pushViewController:unknownPersonViewController animated:YES];

So this adds in a navigation bar, but the back button doesn't do anything except change the title to 'Back'...very helpful lol. Additionally, there are no bounds to my navigation bar in the top, so I'm having a bit of difficulty...images below

navigation bar added programatically

slight issue when trying to go back... lol


Solution

  • You could try presenting it modally like this. I am also not using a navigation bar in the main view. This will add a Cancel button to the top left.

        ABUnknownPersonViewController *view = [[ABUnknownPersonViewController alloc] init];
        view.unknownPersonViewDelegate = self;
    
        //Add person record data
    
        view.displayedPerson = person;
        view.allowsAddingToAddressBook = YES;
    
        UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:view];
    
        view.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] 
           initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self 
           action:@selector(dismissContactView:)];
    
        [self presentViewController:nav animated:YES completion:nil];