当我点击目标c中显示的本地通知时打开特定的视图控制器

时间:2018-05-05 09:46:55

标签: ios objective-c nsnotifications

我想,当我点击呈现给我的本地通知时,应该显示DummyControler的特定视图控制器。

我已经这样做了,但是当我运行我的应用程序时,它失败了。

点击通知

,帮我转到特定的视图控制器
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

if (launchOptions != nil) {
    // Launched from local notification
    NSDictionary *notification = [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];


    UINavigationController * nav = (UINavigationController *)self.window.rootViewController;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //NotificationPreviewViewConroller
    UIViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"DummyViewController"];
    [nav pushViewController:vc animated: false];

 }

 [[UIApplication sharedApplication] registerForRemoteNotifications];    
 return YES;
}

2 个答案:

答案 0 :(得分:0)

您的root控件的类型为UIViewController而不是UINavigationController

请替换

UINavigationController * nav = (UINavigationController *)self.window.rootViewController;
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //NotificationPreviewViewConroller
    UIViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"DummyViewController"];
    [nav pushViewController:vc animated: false];

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //NotificationPreviewViewConroller
    UIViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"DummyViewController"];
    [self.window.rootViewController pushViewController:vc animated: false];

这样可行。

请按照更新后的代码重新编写上述代码。

<强>更新: -

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    //NotificationPreviewViewConroller
    UIViewController *vc=[storyboard instantiateViewControllerWithIdentifier:@"DummyViewController"];
    [self.window.rootViewController.navigationController pushViewController:vc animated: false];

答案 1 :(得分:0)

正如你所说“我想要的,当我点击呈现给我的本地通知时,应该呈现DummyControler的特定视图控制器。”

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
DummyControler *vc=[storyboard instantiateViewControllerWithIdentifier:@"DummyViewController"];
[self.window.rootViewController presentViewController:vc animated:true completion:^{
   //Completion code 
}];