在收到苹果推送通知时从故事板打开一个viewcontroller

时间:2014-02-17 19:13:00

标签: ios objective-c storyboard push-notification

我正在开发一个应用程序,其中包含一个带有3个视图控制器的故事板和启用推送通知的应用程序。当我收到推送通知时,当我点击通知提醒时,它应该从我的故事板中打开第二个视图控制器让我显示我的代码。

-(void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo
{
        [[NSNotificationCenter defaultCenter] postNotificationName:@"pushNotification" object:nil userInfo:userInfo];

}

然后故事板加载实际上是我的第一个视图控制器,它还有一个按钮到第二个视图控制器,这是我想要加载的控制器。这是我第一个视图控制器中的代码。

- (void)viewDidLoad
{
   [super viewDidLoad];
   [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(pushNotificationReceived) name:@"pushNotification" object:nil];
}
-(void)pushNotificationReceived{
    NSString * storyboardName = @"DealerMainStoryboard";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"DealerBuyRequests"];
    [self presentViewController:vc animated:YES completion:nil];
}

因此,当我点击通知时,我收到此代码应用程序的通知时会崩溃。

1 个答案:

答案 0 :(得分:0)

您需要获取一些错误日志,但请检查一下。

UIViewController * vc = 
       [storyboard instantiateViewControllerWithIdentifier:@"DealerBuyRequests"];

我不认为你想创建一个新的UIViewController,除非你真的命名你的控制器" UIViewController "。

因此,请再次检查要以模态方式显示的视图的类名

DealerBuyRequestsViewController * vc = 
       [storyboard instantiateViewControllerWithIdentifier:@"DealerBuyRequests"];

确保此视图控制器的StoryBoard IdDealerBuyRequests匹配,否则您将收到错误。

相关问题