开发者

navigationItem added to ABPersonViewController disappears when app resumed

I created an ABPersonViewController and added a done button:

ABRecordRef rec = ABAddressBookGetPersonWithRecordID(addrBook, recordID);
    if (rec) {
        ABPersonViewController* personController = [[[ABPersonViewController alloc] init] autorelease];
        personController.displayedPerson = rec;
        personController.personViewDelegate = self;
        personController.allowsEditing = NO;

        UIBarButtonItem* doneButton = [[[UIBarButtonItem alloc]
                                       initWithBarButtonSystemItem:UIBarButtonSystemItemDone
                                       target: self
                                       action: @selector(dismissModalView:)] autorelease];

        UINavigationController *navController = [[[UINavigationController alloc] initWithRootViewController:personController] autorelease];
        [self.appViewController presentModalViewController:navController animated: YES];

        // this needs to be AFTER presentModal, if not it does not show up (iOS 4 regressio开发者_如何学Cn: workaround)
        personController.navigationItem.rightBarButtonItem = doneButton;

I should have been suspicious that it had to be added AFTER the view was presented.

This worked until the app was sent to the background with this view active. When the app was restored the done button was no longer there. I've tried many ways to add this Done button but could never get it to remain through an app pause.


Here is the solution. Create an empty UIViewController in front of the ABPersonViewController. This will cause the ABPersonViewController to have a back button rather than the created done button. Override ABPersonViewController (DisplayContactViewController below) so you can implement viewDidDisappear. This will be called when the user presses the back button. In viewDidDisappear you can take down the entire navigation stack (including the empty View controller) and get back to your original view.

    DisplayContactViewController* personController = [[[DisplayContactViewController alloc] init] autorelease];  // 
    personController.displayedPerson = rec; // the ABPersonRecord to display
    personController.personViewDelegate = self;
    personController.allowsEditing = NO;
    personController.contactsPlugin = self;  //this is my hook so I can dismiss the picker view later

    // create this so DisplayContactViewController will have a "back" button.
    UIViewController* parentController = [[[UIViewController alloc] init] autorelease];
    UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:parentController];

    [navController pushViewController:personController animated:YES];

    [self.appViewController presentModalViewController:navController animated: YES];

Here is DisplayContactViewController viewDidDisappear.

[super viewDidDisappear: animated];
[self.contactsPlugin.appViewController dismissModalViewControllerAnimated:NO];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜