简体   繁体   English

从NSObject呈现UIViewController

[英]Present a UIViewController from an NSObject

Something happens in my datamodel, and I need to present a modal viewcontroller instantiated through the storyboard. 我的数据模型中发生了某些事情,我需要提供一个通过情节提要实例化的模式视图控制器。

How can I do this? 我怎样才能做到这一点? I need to present a modal VC from an NSObject, and obviously presentViewController is a UIViewController method. 我需要提供一个来自NSObject的模式VC,显然presentViewController是UIViewController方法。

What's the best way to do this? 最好的方法是什么?

UIStoryboard *mainStoryboard = [(AppDelegate *) [[UIApplication sharedApplication] delegate] storyboard];

NewMessageListenPopupVC *popupVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"NewMessageListenPopupVC"];

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

EDIT: 编辑:

Code I am probably going to end up using: 我可能最终会使用的代码:

UIStoryboard *mainStoryboard = [(AppDelegate *) [[UIApplication sharedApplication] delegate] storyboard];

NewMessageListenPopupVC *popupVC = [mainStoryboard instantiateViewControllerWithIdentifier:@"NewMessageListenPopupVC"];

UIViewController *rootVC = [[(AppDelegate *) [[UIApplication sharedApplication] delegate] window] rootViewController];
[rootVC presentViewController: popupVC animated:YES completion:nil];

rYou could do this by getting the rootView controller from the window of the application and then calling presentViewController on that VC. r您可以通过从应用程序窗口获取rootView控制器,然后在该VC上调用presentViewController来执行此操作。

UIViewController *vc = [window rootViewController];
[vc presentViewController: yourVC animated:YES completion:nil];

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

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