从另一个选项卡调用popToRootViewController

时间:2014-01-09 10:44:01

标签: ios objective-c xcode uitabbarcontroller poptoviewcontroller

我有使用故事板的导航控制器应用程序的标签栏,
我的目的是按下tab3中的按钮,在后台我想要tab1到“popToRootViewController”

tab3 viewcontroller中的按钮:

- (IBAction)Action:(id)sender {

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

}

tab1 viewcontroller中的代码

-(void)popToRootViewController{

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

我在日志中获得popToRootViewController,但操作未执行。

解决问题的

- (IBAction)Action:(id)sender {

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


    }

4 个答案:

答案 0 :(得分:2)

你这样做的方式:

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

不正确。实际上,您在这里创建了一个全新的控制器,完全独立于现有的控制器,不属于任何导航控制器。因此,self.navigationController中的nilpopToRootViewController

您可以尝试执行以下操作:

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

答案 1 :(得分:1)

使用tabBarViewController绑定TabBar-
在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];
}

它非常适合我。

答案 2 :(得分:1)

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

如果您想通过按Tab3中的按钮在tab1中执行popToRootViewController,那么我想建议使用NSNotificationCenter。例如,按照以下代码: -

firstViewController课程中添加NSNotification

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

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

ThirdViewController课程中发布以下代码中的notification: -

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

}

答案 3 :(得分:0)

如果您的tab1和tab2位于不同的navigationController中,请在- (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];
    }
}