【问题标题】:call popToRootViewController from another tab从另一个选项卡调用 popToRootViewController
【发布时间】:2014-01-09 10:44:00
【问题描述】:

我有使用情节提要的导航控制器应用程序的标签栏,
我的目的是在 tab3 中按下一个按钮,在后台我希望 tab1 到“popToRootViewController”

tab3 viewcontroller 中的按钮:

- (IBAction)Action:(id)sender {

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

}

tab1 视图控制器中的代码

-(void)popToRootViewController{

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

我在日志中收到了popToRootViewController,但该操作没有执行。

解决问题:

- (IBAction)Action:(id)sender {

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


    }

【问题讨论】:

    标签: ios objective-c xcode uitabbarcontroller poptoviewcontroller


    【解决方案1】:

    你的做法:

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

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

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

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

    【讨论】:

    • @user2920421:你能检查一下first 得到的值吗?你也会使用这个日志:NSLog(@"popToRootViewController: %@", self.navigationController);...
    【解决方案2】:

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

    它非常适合我。

    【讨论】:

      【解决方案3】:
      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];
      
      }
      

      【讨论】:

        【解决方案4】:

        如果你的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];
            }
        }
        

        【讨论】:

          猜你喜欢
          • 2015-12-03
          • 1970-01-01
          • 1970-01-01
          • 2014-02-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-12-19
          • 1970-01-01
          相关资源
          最近更新 更多