gpt4 book ai didi

iOS 如何在已经堆叠的 ViewController 上调用 presentViewController

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:45:51 25 4
gpt4 key购买 nike

我正在尝试将 View Controller 推送到已经推送的 View Controller 上,如下所示。然而,不知何故,当我尝试推送 newPersonViewController 时,这个新 Controller 并没有被推送到堆栈上。我不知道为什么 Xcode 提示我试图添加一个不属于层次结构的新 Controller 。我是否错误地呈现了 ABPeoplePickerViewController?这就是 newPersonViewController 无法被压入堆栈的原因吗?

来自控制台的消息:

Warning: Attempt to present on whose view is not in the window hierarchy!

代码:

- (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 试图从不是当前 Controller 的 View Controller 中以模态方式呈现。 self(您的原始 View Controller )不是事件 Controller 。 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];
}

关于iOS 如何在已经堆叠的 ViewController 上调用 presentViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14261455/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com