【问题标题】:How to navigate to another view controller in push notifications如何在推送通知中导航到另一个视图控制器
【发布时间】:2015-04-30 09:24:21
【问题描述】:
我正在做一个应用程序。其中我有一些视图控制器,例如 A->B、A->C、A->D 和 B->E、C->F、D->G 和 E- >H,F->I 和 G->J。所以我在 E 视图控制器中,每当收到通知时,我必须移动到 G 视图控制器。如果我在除 G 之外的任何视图控制器中,我需要移动到G view controller.所以请告诉我怎么做。
【问题讨论】:
标签:
ios
iphone
uinavigationcontroller
【解决方案1】:
您需要先在 AppDelegate.m 中设置一些代码,以便在应用打开时响应推送:
- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
//If opening app from notification
if ( application.applicationState == UIApplicationStateInactive || application.applicationState == UIApplicationStateBackground )
{
//restore push
[[NSNotificationCenter defaultCenter] postNotificationName:@"appRestorePush" object:nil];
}
//If app is already open
else {
[[NSNotificationCenter defaultCenter] postNotificationName:@"appOpenPush" object:nil];
}
}
在这里,如果应用通过推送打开(即您从锁定屏幕上滑动),我们会触发 NSNotification,如果应用已经打开,也会有不同的通知。您不必使用两种不同类型的 NSNotification,但它可能对您有用。我不确定您的视图控制器设置是什么,但假设您对根控制器中的所有内容使用 UINavigation 控制器,您只需将其设置为在 ViewDidLoad 中进行侦听,即:
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appOpenPush) name:@"appOpenPush" object:nil];
在你调用的方法中是这样的:
-(void)appOpenPush {
NSLog(@"got a push while app was open");
//Get the view controller
UIViewController *lastViewController = [[self.navigationController viewControllers] lastObject];
if([lastViewController isKindOfClass:[MyViewController class]]) {
//do your segue here
}
else if//////do this for all your cases...
}
这里我们检查视图控制器是什么类型的类,然后选择合适的segue。
希望你至少做了一点工作,并理解我写的内容..