我知道這個問題已被問了很多次。但我是一個新手,我無法找到任何解決方案來幫助我解決問題。這裏有一個簡單的解釋:如何從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
。
試試這個,並修改它爲你的情況http://stackoverflow.com/questions/10015567/how-do-i-access-my-viewcontroller-from-my-appdelegate-ios/29773513#29773513 –