簡體   English   中英

如何從AppDelegate打開特定的ViewController?

[英]How to open specific ViewController from AppDelegate?

我知道這個問題已經被問過很多次了。 但是我是一個新手,我無法找到任何解決方案來幫助我。 這里是一個簡短的解釋:

我有一個以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

如您所言,您正在將TabBarController與Storyboard一起使用。 那你為什么要再次初始化? 您可以按照以下步驟做

- (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。

假設您已在情節提要板中設計(拖放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訪問它們並更改其屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM