从推送通知IOS打开应用程序时打开特定的ViewController

时间:2015-10-21 09:44:51

标签: ios parsing uinavigationcontroller push tabbarcontroller

我需要从推送通知中打开一个特定的视图控制器; 这是我的主要律师:

MyStoryBoard

我能够到达ViewController,但当我在那里时,我看不到tabbar和导航控制器。 这里是代码:

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
if (launchOptions != nil)
{
        UIStoryboard*  sb = [UIStoryboard storyboardWithName:@"Main"
                                                      bundle:nil];
        UIViewController* vc = [sb instantiateViewControllerWithIdentifier:@"Table1"];

        self.window.rootViewController = vc;

}
return YES;

}

任何人都可以帮助我吗?

4 个答案:

答案 0 :(得分:1)

我认为你想要" NAVIGATE"点击通知后到特定的ViewController。如果那是正确的那么您需要从根导航控制器到目标视图控制器,为此您可能需要在检查它是否为&#39之后将该最终视图控制器推送到应用程序的当前导航堆栈上。 ; s已经存在于堆栈中,如果它已经存在,你只需更新它,否则推送ViewController。

目前,您的代码似乎只是将最终视图控制器设置为根视图控制器,这就是您没有看到导航栏/标签栏的原因。

如果您需要有关代码的帮助,请告知我们我会更新我的回答。 祝你好运

答案 1 :(得分:0)

如果您的意思是 - 应该选择另一个标签,您可以使用以下内容:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    if (launchOptions)
    {
        UITabBarController* controller = (UITabBarController*)self.window.rootViewController;
        if ([controller isKindOfClass:[UITabBarController class]])
        {
            [controller setSelectedIndex:1]; // Here you should specify correct tab's number
        }

    }

    return YES;
}

否则 - 你能更多地描述你的问题吗?

答案 2 :(得分:0)

在App委托中,如果标签栏是初始视图控制器或相应地遍历标签栏控制器,则可以将其置于didReceiveLocalNotification或didRecieveRemoteNotification方法下。

MenuTabBarViewController *controller = (MenuTabBarViewController *)self.window.rootViewController;

controller.selectedViewController = [controller.viewControllers objectAtIndex:1]; // The tab you want to show.

答案 3 :(得分:0)

检查launchOptions-Dictionary

中的启动选项键UIApplicationLaunchOptionsRemoteNotificationKeyUIApplicationLaunchOptionsLocalNotificationKey

然后从通知userinfo获取详细信息,并使用rootViewController获取currentViewController。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window.rootViewController = ...//init viewController in storyboard or coed;
    UILocalNotification *notification = launchOptions[UIApplicationLaunchOptionsLocalNotificationKey];
    if (notification){
        // do something with notification userinfo
        UIViewController *rootController = self.window.rootViewController;
        UIViewController *currentController = rootController;

        if ([currentController isKindOfClass:[UITabBarController class]]) {
            currentController = [(UITabBarController *)currentController selectedViewController];
        }

        if ([currentController isKindOfClass:[UINavigationController class]]){
            [(UINavigationController *)currentController pushViewController:newVC animated:YES];
        }else if ([currentController isKindOfClass:[UIViewController class]]){
            [currentController.navigationController pushViewController:newVC animated:YES];
        }
    }
}

如果您的应用只在后台,请改用- (void)application:(UIApplication *)application didReceiveLocalNotification:(UILocalNotification *)notification- (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo