Present a UIViewController from an NSObject

我是研究僧i 提交于 2019-12-11 04:47:23

问题


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.

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

回答1:


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


来源:https://stackoverflow.com/questions/19438538/present-a-uiviewcontroller-from-an-nsobject

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!