简体   繁体   English

如何从AppDelegate打开特定的ViewController?

[英]How to open specific ViewController from AppDelegate?

I know this question has been asked many times. 我知道这个问题已经被问过很多次了。 But I am a newbie and I was not able to find any solutions to help me in my condition. 但是我是一个新手,我无法找到任何解决方案来帮助我。 Here is a brief explanation: 这里是一个简短的解释:

I have an app with a UITabBarController as root. 我有一个以UITabBarController为根的应用程序。

Inside the app delegate I check if user is already logged in. If yes I will open root controller. 在应用程序委托中,我检查用户是否已经登录。如果是,我将打开根控制器。

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

My application is working fine. 我的应用程序运行正常。 But now I need to implement notification. 但是现在我需要实现通知。 When I get notification content inside didFinishLaunchingWithOptions : 当我在didFinishLaunchingWithOptions内部获得通知内容时:

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

Now if there is a message: 现在,如果有消息:

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];

}

This piece of code above did not work for me. 上面的这段代码对我不起作用。 And also I don't know how to open different tabs and give value to their badges form here. 而且我也不知道如何在此处打开不同的标签并为其徽章表单赋予价值。 It always navigate to the first index tab with these code. 它始终使用这些代码导航到第一个索引选项卡。 I have seen other answers say: 我看到其他答案说:

self.tabBarController.selectedIndex = 2;

But did not work for me. 但是对我没有用。 I have implemented an UITabBarController class and I can set value for the each tab item badges from there but I get my notificationPayLoad in AppDelegate. 我已经实现了UITabBarController类,并且可以从那里设置每个选项卡项徽章的值,但是我在AppDelegate中获得了notificationPayLoad

As you told that you are using TabBarController with storyboard. 如您所言,您正在将TabBarController与Storyboard一起使用。 Then why are you initialising again? 那你为什么要再次初始化? You can just do as following 您可以按照以下步骤做

- (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;
}

Here I am just showing a demo code for accessing TabbarController at different selectedIndex within didFinishLaunching. 在这里,我只是演示一个演示代码,用于在didFinishLaunching中的不同selectedIndex上访问TabbarController。

Assuming that you have designed(drag n dropped UITabBar/ UITabBarController) in storyboard. 假设您已在情节提要板中设计(拖放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
        }
    }


}

Views designed on story board are already initialized, we can access them using the IBOutlet and change their properties. 故事板上设计的视图已经初始化,我们可以使用IBOutlet访问它们并更改其属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM