繁体   English   中英

错误的“presentingViewController”

[英]Wrong 'presentingViewController'

在我的MainViewController中,我通过这个展示了另一个视图控制器:

MessageViewController *messageController = [[MessageViewController alloc]initWithNibName:nil bundle:nil];

[messageController setModalPresentationStyle:UIModalPresentationFullScreen];
[messageController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentViewController:messageController animated:YES completion:nil];

[messageController release];

这将正确显示视图控制器。 但是,当我尝试返回呈现视图控制器(在本例中应该是MainViewController )时,此代码不起作用:

if ([self.presentingViewController isKindOfClass:[MainViewController class]])
    [(MainViewController *)self.presentingViewController setCurrentViewTag:2];

[self dismissModalViewControllerAnimated:YES];

我删除了“if..”条件以强制它设置当前视图标签。 发生错误告诉我呈现的视图控制器似乎是UINavigationController

[UINavigationController setCurrentViewTag:]: unrecognized selector sent to instance 0x8352a50

谁能告诉我为什么会这样? 这段代码以前可以工作,我不确定是什么改变使它停止正常工作。

编辑

这是更新后的代码:

ReaderController *readerController = [[ReaderController alloc]initWithNibName:nil bundle:nil];
[readerController loadWhichViewToShow:2];

[self setDefinesPresentationContext:YES];

[readerController setModalPresentationStyle:UIModalPresentationFullScreen];
[readerController setModalTransitionStyle:UIModalTransitionStyleFlipHorizontal];

[self presentViewController:readerController animated:YES completion:nil];

[readerController release];

调用[self presentViewController:messageController animated:YES completion:nil]; 不一定使用你调用它的 vc 来呈现另一个 vc。 默认情况下,它向上移动 vc 层次结构并在根视图控制器上显示另一个 vc。 这就是为什么在您的情况下呈现视图控制器是UINavigationController

如果你想强制你的 MainViewController 成为呈现的 vc,你有调用:

[self setDefinesPresentationContext:YES];

在呈现 MessageViewController 之前在您的 MainViewController 上。

编辑:以防其他人读到: definesPresentationContext似乎被窃听或文档有误。 请参阅下面的评论和Cocoa Builder

我从这个问题的答案的副本

来自编程 iOS 6,作者 Matt Neuburg

在 iPad 上,当呈现的视图控制器的 modalPresentationStyle 是 UIModalPresentationCurrentContext 时,必须决定哪个视图控制器应该是呈现的视图控制器的 presentingViewController。 这将确定哪个视图将被呈现的视图控制器的视图替换。 这个决定涉及另一个 UIViewController 属性,definesPresentationContext(一个 BOOL)。 从 presentViewController:animated:completion: 被发送到的视图控制器开始,我们沿着父视图控制器链向上走,寻找 definesPresentationContext 属性为 YES 的视图控制器。 如果我们找到一个,那就是那个; 它将是 presentingViewController,它的视图将被呈现的视图控制器的视图所取代。 如果我们没有找到,事情就好像呈现的视图控制器的 modalPresentationStyle 是 UIModalPresentationFullScreen 一样。

长话短说
1. 在所需的presentingViewController definesPresentationContext设置为 true
2. 在所需的presentedViewController上将UIModalPresentationCurrentContext设置为modalPresentationStyle

如果您似乎需要在 iOS 11 中设置三件事。

controller.modalPresentationStyle = UIModalPresentationCurrentContext;
controller.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
self.definesPresentationContext = YES;
[self presentViewController:controller animated:YES completion:nil];

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM