当我点击通知时,我希望它打开应用程序的通知viewController。所有代码都在AppDelegate.m中
我有一个函数来找到topMostController
- (UIViewController *)topMostController {
UIViewController *topController = [UIApplication sharedApplication].keyWindow.rootViewController;
while (topController.presentedViewController) {
topController = topController.presentedViewController;
}
return topController;
}
然后还有另一个显示通知viewController的函数
UINavigationController *navigationController = (UINavigationController *)[self topMostController];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryBoard" bundle:nil];
MyViewController *myController = [storyboard instantiateViewControllerWithIdentifier:@"MyViewController"];
[navigationController pushViewController:myController animated:YES];
如果应用程序打开,它工作正常。但是,当应用程序关闭时,如果我单击通知,它将崩溃。错误是
Fatal Exception: NSInvalidArgumentException
-[ViewController pushViewController:animated:]: unrecognized selector sent to instance
我认为没有topMostController或者navigationController在这种情况下是无效的。我需要使用UINavigationController,因为我想要一个“后退”按钮返回页面(主页面)而不是关闭应用程序。在这种情况下,如何使topMostController有效?我应该在didFinishLaunchingWithOptions函数中创建一些东西吗?谢谢。
你需要分配根视图控制器
self.navigationController = RootNavigationControllerViewController(rootViewController: HomeViewController.init())
self.window?.rootViewController = self.navigationController
self.window?.makeKeyAndVisible()