iOS通过本地通知显示特定的viewcontroller

时间:2013-12-03 16:36:06

标签: ios objective-c uiviewcontroller uilocalnotification

我已经在我的应用中实现了本地通知,但我想选择在用户使用哪个viewcontroller时显示"刷卡"通知。我的应用程序是一些视图控制器,它们之间有基本的segue导航。

如何选择要查看的视图控件?

3 个答案:

答案 0 :(得分:3)

-(void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification
{
    [[UIApplication sharedApplication] cancelLocalNotification:notification];


    //My_specificViewController
    RingingViewController *ringingVC = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:@"RingingViewController"];
    [self.window setRootViewController:ringingVC];
}

答案 1 :(得分:0)

您可以在应用程序中处理本地通知:didFinishLaunchingWithOptions方法,之后您可以选择正确的视图控制器:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Handle notification
    UILocalNotification *localNotif =
    [launchOptions objectForKey:UIApplicationLaunchOptionsLocalNotificationKey];
    UIViewController *vc = nil
    if (localNotif) {
        //base on notification create right view controller
        vc = [[VC alloc] init];
        NSLog(@"Recieved Notification %@",localNotif);
    }
    else
    {
       //create default view controller
        vc = [[VC alloc] init];
    }

    // Add the view controller's view to the window and display.
    _window.rootViewController = vc;
    [_window makeKeyAndVisible];

    return YES;
}

答案 2 :(得分:0)

Greg为您提供了大部分答案,但请注意除了didFinishLaunchingWithOptions之外,您还需要添加一个应用程序:didReceiveLocalNotification:method。一旦你的应用程序运行,就会调用该方法。我建议拔出代码来处理单个方法中的本地通知并从两个地方调用它。