gpt4 book ai didi

ios - 在 IOS 上,parentViewController 返回 null

转载 作者:行者123 更新时间:2023-12-01 15:59:02 25 4
gpt4 key购买 nike

我正在使用以下代码以模态方式呈现 View Controller :

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
SubmitAYoNViewController *ivc = [storyboard instantiateViewControllerWithIdentifier:@"SubmitAYoN"];
[ivc setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:ivc animated:YES completion:nil];

然后在我的 SubmitAYoNViewController 中我有这个:

  NSLog(@"%@",self.parentViewController);
if([self.parentViewController isKindOfClass:[YesOrNoViewController class]]) {
NSLog(@"do something");
}

self.parentVeiwController 为 NULL。这是为什么?

编辑:我想从我的父 View Controller 访问一种方法,然后访问打开 SubmitAYoN 的方法。

解决方案:我使用了委托(delegate)。根据下面的答案,presentViewController 不会创建父子关系。

最佳答案

如果您想要呈现 View Controller ,请使用self.presentingViewController。 self.parentViewController 返回 Controller 父级,当它不是另一个 Controller 的子级时,该父级将为 nil。

所以使用:

NSLog(@"%@",self.presentingViewController);
if([self.presentingViewController isKindOfClass:[YesOrNoViewController class]]) {
NSLog(@"do something");
}

来自 presentViewController 的文档:

This method sets the presentedViewController property to the specified view controller, resizes that view controller's view based on the presentation style and then adds the view to the view hierarchy.

因此没有提及 Controller 被设为子级,因为它是添加到现有 View 层次结构中的 View 。

在 iOS5 之前你应该没问题。属性 parentViewController 的文档指出:

Prior to iOS 5.0, if a view did not have a parent view controller and was being presented, the presenting view controller would be returned. On iOS 5, this behavior no longer occurs. Instead, use the presentingViewController property to access the presenting view controller.

关于ios - 在 IOS 上,parentViewController 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30507339/

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