从 AppDelegate 重定向到特定屏幕:点击通知
Posted
技术标签:
【中文标题】从 AppDelegate 重定向到特定屏幕:点击通知【英文标题】:Redirect to Specific screen from AppDelegate : On tap Notification 【发布时间】:2016-08-16 14:04:40 【问题描述】:用于消息应用程序。
结构是
TabBarController -> NavigationController->View_One -> View_Two。每当用户在通知选项卡上时,我需要从 appdelegate.m [DidReceiveRemoteNotification method]重定向到 View_2。
这个当前代码帮助我登陆 View_One。如何在不影响 NavigationController 和 Tabbar 控制器功能的情况下转到 View_Two?
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
if(application.applicationState == UIApplicationStateInactive||application.applicationState == UIApplicationStateBackground)
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:MAIN_STORYBOARD bundle:nil];
UITabBarController *tabBarController = [mainSB instantiateViewControllerWithIdentifier:TAB_BAR_ID];
tabBarController.selectedIndex = 0;
[[UIApplication sharedApplication].keyWindow setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
提前致谢。
【问题讨论】:
它打开到你的2
索引
编辑了我的问题,请检查一次。
【参考方案1】:
试试这个
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
if(application.applicationState == UIApplicationStateInactive||application.applicationState == UIApplicationStateBackground)
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:MAIN_STORYBOARD bundle:nil];
UITabBarController *tabBarController = [mainSB instantiateViewControllerWithIdentifier:TAB_BAR_ID];
tabBarController.selectedIndex = 0;
UINavigationController *nav = [tabBarController.viewControllers objectAtIndex:0];
View2 *destViewController = (View2*) [self.nav.viewControllers objectAtIndex:0];
[nav pushViewController:destViewController animated:YES];
选择 2
我不确定上述方法是否有效,在这里我们使用一些 hacky 方法,在这里根据您的代码,我们使用NSUSerdefault
概念
-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler
if(application.applicationState == UIApplicationStateInactive||application.applicationState == UIApplicationStateBackground)
UIStoryboard *mainSB = [UIStoryboard storyboardWithName:MAIN_STORYBOARD bundle:nil];
UITabBarController *tabBarController = [mainSB instantiateViewControllerWithIdentifier:TAB_BAR_ID];
tabBarController.selectedIndex = 0;
[[NSUserDefaults standardUserDefaults]setObject:@"APNS" forKey:@"openVC"]
[[UIApplication sharedApplication].keyWindow setRootViewController:tabBarController];
[self.window makeKeyAndVisible];
检索喜欢
- (void)viewDidLoad // or call in loadView Method
if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"openVC"]isEqualToString:@"APNS"])
// Navigate to second VC
终于
在你的第二个VC上
-(void)viewWillDisappear:(BOOL)animated
[[NSUserDefaults standardUserDefaults] removeObjectForKey:@"openVC"];
[super viewWillDisappear:animated];
【讨论】:
Tq。在这里你没有提到 RootViewController。还好吗? @Ramdy - 没必要,试一次,如果你没有得到答案,我们会采取其他一些棘手的方法。 在第一种方法中,View2 destViewController = (View2) [self.nav.viewControllers objectAtIndex:0];此代码返回 View1 的引用。 让我们continue this discussion in chat。 是的,我使用了第二个选择-2。工作正常。以上是关于从 AppDelegate 重定向到特定屏幕:点击通知的主要内容,如果未能解决你的问题,请参考以下文章
如何在通知点击时重定向到特定的 ViewController?