2015-11-03 78 views
0

我知道這個問題已被問了很多次。但我是一個新手,我無法找到任何解決方案來幫助我解決問題。這裏有一個簡單的解釋:如何從AppDelegate打開特定的ViewController?

我有一個應用程序與UITabBarController作爲根。

在應用程序委託內部,我檢查用戶是否已經登錄。如果是,我將打開根控制器。

self.window.rootViewController = [[UIStoryboard storyboardWithName:@"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController]; 

我的應用程序工作正常。但現在我需要實施通知。當我進去didFinishLaunchingWithOptions通知內容:

NSDictionary *notificationPayload = launchOptions[UIApplicationLaunchOptionsRemoteNotificationKey]; 
NSString *messageType = [notificationPayload objectForKey:@"messageType"]; 

現在如果有一個消息:

if (messageType.length > 0) 
{ 
    //Here based on message I need to open different tabs of TabBarViewController 

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController; 
    //RootTabBarViewController *listingVC = [[RootTabBarViewController alloc] init]; 
    [(UINavigationController *)self.window.rootViewController pushViewController:tabBarController animated:YES]; 

} 

這段代碼上面並沒有爲我工作。 而且我也不知道如何打開不同的標籤,並在這裏給他們的徽章形式賦予價值。它總是使用這些代碼導航到第一個索引選項卡。我看到其他答案說:

self.tabBarController.selectedIndex = 2; 

但沒有爲我工作。我已經實現了一個UITabBarController類,我可以從那裏爲每個標籤項徽章設置值,但是我在AppDelegate中獲得了我的notificationPayLoad

+0

試試這個,並修改它爲你的情況http://stackoverflow.com/questions/10015567/how-do-i-access-my-viewcontroller-from-my-appdelegate-ios/29773513#29773513 –

回答

0

正如您所說的,您正在使用TabBarController與故事板。那你爲什麼要再次初始化?你可以做如下

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { 
    // Override point for customization after application launch. 
    if([[NSUserDefaults standardUserDefaults]boolForKey:@"Selected"]) 
    { 
     [[NSUserDefaults standardUserDefaults]setBool:NO forKey:@"Selected"]; 
     UITabBarController *tabController = (UITabBarController*)self.window.rootViewController; 
     tabController.selectedIndex=1; 
    }else{ 
     [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"Selected"]; 
     UITabBarController *tabController = (UITabBarController*)self.window.rootViewController; 
     tabController.selectedIndex=0; 
    } 
    return YES; 
} 

我在這裏只是顯示在中didFinishLaunching不同的selectedIndex訪問TabbarController演示代碼。

0

假設您已經設計了(在故事板中拖動n拖放的UITabBar/UITabBarController)。

if (messageType.length > 0) 
{ 
    //Here based on message I need to open different tabs of TabBarViewController 



    NSUInteger indexOfRequiredTab = 2;// pass the index of controller here 

    id rootObject = self.window.rootViewController; 
    if ([rootObject isKindOfClass:[UITabBarController class]]) { 

     UITabBarController *tabBarControllerObject = (UITabBarController *)rootObject; 

     if (indexOfRequiredTab != NSNotFound && indexOfRequiredTab < tabBarControllerObject.tabBar.items.count) { 

      tabBarControllerObject.tabBar.items[indexOfRequiredTab].badgeValue = @"2"; //to set badge value 

      tabBarControllerObject.selectedIndex = indexOfRequiredTab; //Setting this property changes the selected view controller to the one at the given index in the viewControllers array 
     } 
    } 


} 

查看設計在故事板已經被初始化,我們可以使用IBOutlet訪問它們並改變它們的屬性。

相關問題