从NSObject呈现UIViewController [英] Present a UIViewController from an NSObject

查看:112
本文介绍了从NSObject呈现UIViewController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据模型中发生了某些事情,我需要展示一个通过情节提要实例化的模式视图控制器.

Something happens in my datamodel, and I need to present a modal viewcontroller instantiated through the storyboard.

我该怎么做?我需要提供一个来自NSObject的模式VC,显然presentViewController是UIViewController方法.

How can I do this? I need to present a modal VC from an NSObject, and obviously presentViewController is a UIViewController method.

做到这一点的最佳方法是什么?

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];

我可能最终会使用的代码:

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];

推荐答案

r您可以通过从应用程序窗口获取rootView控制器,然后在该VC上调用presentViewController来做到这一点.

rYou could do this by getting the rootView controller from the window of the application and then calling presentViewController on that VC.

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

这篇关于从NSObject呈现UIViewController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆