[英]How do you change the initial view controller in an ios application based on a remote notification?
I have an application that is implementing remote notifications. 我有一个正在实现远程通知的应用程序。 My desire is to change the initial view controller based on one of three conditions: the user started the app on their own (no notifications);
我的愿望是根据以下三个条件之一来更改初始视图控制器:用户自行启动应用程序(无通知); or, the user received an suspicious activity alert;
或者,用户收到了可疑的活动警报; or, the user received a crime alert.
或者,用户收到了犯罪警报。 I have implemented the application didReceiveRemoteNotification: method in the appDelegate.
我已经在appDelegate中实现了应用程序didReceiveRemoteNotification:方法。 Based on the notification and the user response to the notification I have implemented the following code:
基于通知和用户对通知的响应,我实现了以下代码:
UITabBarController *tbContr = (UITabBarController*) self.window.rootViewController;
UINavigationController *navContr = [tbContr.viewControllers][2];
ViewCrimesController *viewCrimes = [navContr.storyboard instantiateViewControllerWithIdentifier:@"ViewCrimes"];
[navContr presentViewController:viewCrimes animated:YES completion:nil];
[self.window.makeKeyAndVisible];
The problem I am having is that the navigation controls -- ie, the back button and the navigation bar title; 我遇到的问题是导航控件-即返回按钮和导航栏标题; are not on the ViewCrimesController when it is presented.
呈现时不在ViewCrimesController上。 I have tried to load the ViewCrimesController many different ways.
我试图以多种不同方式加载ViewCrimesController。 Each way I get either and error saying there is no segue (this view is a model view to map view) or I am attempting to load the over an active view or, again, I don't get the navigation controls.
无论哪种方式,我都会收到错误提示说没有segue(此视图是映射视图的模型视图),或者我试图在活动视图上加载该视图,或者再次没有导航控件。
Do I need to specifically program the navigation controls or am I missing something in the way I am attempting to load the view? 我是否需要对导航控件进行专门编程,还是在尝试加载视图时缺少某些东西?
I have seen references to dynamically changing the initial view in other posts. 我在其他文章中看到了有关动态更改初始视图的参考。 But I have not seen anything that indicates specific programming is required to add the controls.
但是我还没有看到任何指示需要添加控件的特定编程的东西。 Any help you can provide is greatly appreciated!
您能提供的任何帮助将不胜感激!
Susan 苏珊
Have you looked at the post, Programmatically set the initial view controller using Storyboards 您是否看过帖子, 使用情节提要以编程方式设置初始视图控制器
The answer from that post may solve your issue, 该帖子的答案可能会解决您的问题,
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *viewController = // determine the initial view controller here and instantiate it with [storyboard instantiateViewControllerWithIdentifier:<storyboard id>];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
return YES;
}
You can also download this sample, and implement it as per your requirement. 您还可以下载此样本,然后根据需要实施它。
simply change the key window's rootViewController, sample code placed here, hope it helps: 只需更改键窗口的rootViewController,示例代码放在此处,希望对您有所帮助:
UIStoryboard *storyboard = self.window.rootViewController.storyboard;
UIViewController *rootViewController = [storyboard instantiateViewControllerWithIdentifier:@"rootNavigationController"];
self.window.rootViewController = rootViewController;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.