我试图将一个视图控制器推到一个已经被推的视图控制器上,如下所示。但是,以某种方式,当我尝试推送newPersonViewController时,此新控制器不会被推送到堆栈中。我不知道为什么Xcode抱怨我试图添加一个不属于该层次结构的新控制器。我输入的ABPeoplePickerViewController不正确吗?这是为什么newPersonViewController无法推入堆栈的原因吗?

来自控制台的消息:

警告:尝试显示不在窗口层次结构中的视图!

代码:

- (void)addPerson
{
    ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
    newPersonVC.newPersonViewDelegate = self;
    UINavigationController *uiNav = [[UINavigationController alloc] initWithRootViewController:newPersonVC];

    [self presentViewController:uiNav animated:YES completion:nil];
}

- (void)showPeoplePicker
{
    ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
    picker.peoplePickerDelegate = self;

    [self presentViewController:picker animated:YES completion:nil];
    [picker.topViewController setEditing:YES];
    picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(addPerson)];
    picker.topViewController.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(peoplePickerCancel)];
}

最佳答案

问题是addPerson试图从不是当前控制器的视图控制器中以模态形式呈现。 self(您的原始视图控制器)不是 Activity 控制器。 ABPeoplePickerNavigationController是。

因此,首先,定义一个类属性来跟踪picker:

@property (nonatomic, weak) ABPeoplePickerNavigationController *picker;

然后,
  • 修改showPeoplePicker以设置该属性;和
  • 修改addPerson以使用该属性:

  • 从而:
    - (void)addPerson
    {
        ABNewPersonViewController *newPersonVC = [[ABNewPersonViewController alloc] init];
        newPersonVC.newPersonViewDelegate = self;
        UINavigationController *uiNav = [[UINavigationController alloc] initWithRootViewController:newPersonVC];
    
        [self.picker presentViewController:uiNav animated:YES completion:nil];
    }
    
    - (void)showPeoplePicker
    {
        ABPeoplePickerNavigationController *picker = [[ABPeoplePickerNavigationController alloc] init];
        picker.peoplePickerDelegate = self;
    
        [self presentViewController:picker animated:YES completion:nil];
        [picker.topViewController setEditing:YES];
        picker.topViewController.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd    target:self action:@selector(addPerson)];
        picker.topViewController.navigationItem.leftBarButtonItem  = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(peoplePickerCancel)];
    
        self.picker = picker;
    }
    
    - (void)newPersonViewController:(ABNewPersonViewController *)newPersonView didCompleteWithNewPerson:(ABRecordRef)person
    {
        [newPersonView dismissViewControllerAnimated:YES completion:nil];
    }
    

    10-06 12:49