简体   繁体   中英

calling UIViewController through UITabBarController for push

In my app I have UINavigationController as initial view , Hierarchy is like this

UINavigationController-->LoginViewController -->UITabBarController-->UINavigationController -->MasterViewController

MasterViewController has tableView in itself

when a push notification comes , I click on push notification while app is working at background , then app opens masterViewController, the problem is that I want to update tableList in MasterviewController when I open the app from push notification.

I try to navigate to MasterViewController from AppDelegate like this

 -(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

    UINavigationController * navController = (UINavigationController *) self.window.rootViewController;

    MasterViewController * masterController = [navController.viewControllers objectAtIndex:0];

    [masterController updateList];

    }

It is not working, How can I update MasterViewController when I get the push?

Thanx.

You should try in this way:

UINavigationController * navController = (UINavigationController *) self.window.rootViewController;
for (id viewController in navController.viewControllers) {
    if ([viewController isKindOfClass:[MasterViewController class]]) {

              MasterViewController * masterController = (MasterViewController*) viewController; 
              [masterController updateList];
             break;
    }
}

This could work as well.

-(void) application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo {

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
    MasterViewController * masterController = [storyboard instantiateViewControllerWithIdentifier:@"Your_StoryboardID"];
[masterController updateList];
}
UINavigationController * navController = (UINavigationController *) self.window.rootViewController;

MasterViewController * masterController = [navController visibleViewController];
if ([viewController isKindOfClass:[masterController class]]) {
    [masterController updateList];
}
eles{
   //Hierarchy maybe not as your description
}

Getting the tabbar view controllers and call that view method with pop...

    NSArray *array=[self.tabBarViewController viewControllers];

                    for(int i=0;i<array.count;i++)
                    {

                        UINavigationController *navCont=[array objectAtIndex:i];
                        NSArray *navArray=[navCont viewControllers];
                        for (id view in navArray)
                        {
                            if ([view isKindOfClass:[MasterViewController class]])
                            {
                                MasterViewController * masterController =        (MasterViewController*) viewController; 
                                 [masterController updateList];

//if you want to pop to that view controller then use below two lines of code else put comments..

                                [self.tabBarViewController setSelectedIndex:0]; 
                                [navCont popToRootViewControllerAnimated:YES];
                            }
                            break;
                         }
                   }

Hope it will solve your issue....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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