添加到ABPersonViewController的navigationItem在应用程序恢复时消失

时间:2011-09-09 21:17:14

标签: ios

我创建了一个ABPersonViewController并添加了一个完成按钮:

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 regression: workaround)
        personController.navigationItem.rightBarButtonItem = doneButton;

我应该怀疑它必须在视图出现后添加。

这一直有效,直到应用程序被发送到后台并且此视图处于活动状态。应用程序恢复后,完成按钮不再存在。我已经尝试了很多方法来添加这个Done按钮但是永远无法通过应用程序暂停来保持它。

1 个答案:

答案 0 :(得分:2)

这是解决方案。在ABPersonViewController前面创建一个空的UIViewController。这将导致ABPersonViewController具有后退按钮而不是创建的完成按钮。覆盖ABPersonViewController(下面的DisplayContactViewController),以便您可以实现vi​​ewDidDisappear。当用户按下后退按钮时将调用此方法。在viewDidDisappear中,您可以删除整个导航堆栈(包括空的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];

这是DisplayContactViewController viewDidDisappear。

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