call popToRootViewController from another tab

半城伤御伤魂 提交于 2019-12-11 09:25:37

问题


I have tab bar with navigation controller app using storyboard ,
my purpose is to press a button in tab3 and in the background I want tab1 to "popToRootViewController"

the button in tab3 viewcontroller:

- (IBAction)Action:(id)sender {

    vc1 * first = [[vc1 alloc]init];
    [first performSelector:@selector(popToRootViewController) withObject:Nil];

}

the code in the tab1 viewcontroller

-(void)popToRootViewController{

    [self.navigationController popToRootViewControllerAnimated:NO];
    NSLog(@"popToRootViewController");
}

I get the popToRootViewController in logs, but the action didn't perform.

that solve the problem:

- (IBAction)Action:(id)sender {

        [[self.tabBarController.viewControllers objectAtIndex:0]popToRootViewControllerAnimated:NO];


    }

回答1:


The way you are doing it:

vc1 * first = [[vc1 alloc]init];
[first performSelector:@selector(popToRootViewController) withObject:Nil];

is not correct. Indeed, you are creating a whole new controller here, completely independent from your existing controllers and not belonging to any navigation controller. For this reason, self.navigationController is nil in popToRootViewController.

You might try doing something like:

 //-- this will give you the left-most controller in your tab bar controller
 vc1 * first = [self.tabBarController.viewControllers objectAtIndex:0];
[first performSelector:@selector(popToRootViewController) withObject:Nil];



回答2:


Bind TabBar with tabBarViewController-
In tabBarViewController.m

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    NSArray *array = [tabBarController viewControllers];
    if([[array objectAtIndex:tabBarController.selectedIndex] isKindOfClass:[UINavigationController class]])
        [(UINavigationController *)[array objectAtIndex:tabBarController.selectedIndex] popToRootViewControllerAnimated: NO];
}

It worked perfectly for me.




回答3:


To press a button in tab3 and in the background I want tab1 to "popToRootViewController"

If you want to perform popToRootViewController in tab1 by pressing button in tab3 then i would like to suggest use NSNotificationCenter. For example follow below code:-

In your firstViewController class add the observer of NSNotification

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(yourMethod:)
                                                name:@"popToRootViewControllerNotification" object:nil];
}

-(void)yourMethod:(NSNotification*)not
{
 [self.navigationController popToRootViewControllerAnimated:NO];
}

In your ThirdViewController class post the notification in below code:-

- (IBAction)Action:(id)sender {

 //   vc1 * first = [[vc1 alloc]init];
  //  [first performSelector:@selector(popToRootViewController) withObject:Nil];

//Post your notification here
[[NSNotificationCenter defaultCenter] postNotificationName:@"popToRootViewControllerNotification" object:nil];

}



回答4:


If your tab1 and tab2 arein different navigationController, then try this in - (IBAction)action:(id)sender

NSArray *viewControllers = [self.tabbarController viewControllers];
for (UIViewController *viewController in viewControllers) {
    if ([viewController isKindOfClass:[vc1 class]]) {
        vc1 * first = (vc1 *) viewController;
        [first.navigationController popToRootViewControllerAnimated:NO];
    }
}


来源:https://stackoverflow.com/questions/21017985/call-poptorootviewcontroller-from-another-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!