作者热门文章
- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试将 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/
我是一名优秀的程序员,十分优秀!