【问题标题】:How to catch Programmatically changed tabBarController setSelectedIndex如何捕获以编程方式更改的 tabBarController setSelectedIndex
【发布时间】:2012-04-27 10:41:30
【问题描述】:

在我的应用程序中,我有一种情况,我像这样以编程方式更改 tabBarController 的选定索引

[self.tabBarController setSelectedIndex:0];

然后返回到包含 UITableView 的选项卡,此时我需要对我的 tableView 进行一些操作 我尝试使用 viewWillAppear 但它没有到达那里,我试图让视图委托像这样

@interface ThirdViewController : UITableViewController < UISearchBarDelegate,UINavigationControllerDelegate, UITabBarDelegate> 

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{  

没有成功

有人知道怎么做吗?

【问题讨论】:

    标签: iphone ios ipad uitabbarcontroller uitabbar


    【解决方案1】:

    我注意到你的类ThirdViewController实现了UITabBarDelegate,而tabBarController:didSelectViewController:实际上是UITabBarControllerDelegate协议的一个方法。

    【讨论】:

    • tabBarController:didSelectViewController: 仅通过 UITabBarController 的 ui 事件(单击)调用,使用 [self.tabBarController setSelectedIndex:0] 时我没有;我试过了,但它不起作用
    【解决方案2】:

    试试下面的代码:

    self.tabBarController.selectedIndex = 1;
    

    [self.tabBarController setSelectedIndex:1];
    

    谢谢..!

    更新:

    您可以在下面的代码中为 tabBarcontroller viewdidLoad 方法设置委托:

    tabBarController.delegate=self;
    

    【讨论】:

    • 这就是我现在所做的,我想在我跳转到的视图中捕捉它
    • 我试过你的更新,如果我点击按钮,它会工作,但我使用 [self.tabBarController setSelectedIndex:0];然后是未调用的函数
    • 好吧试试这个方法 - (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item{}
    • 谢谢你非常多的dinesh你投入回答我的问题,但是在被编程方式完成时未激活此功能,对我的解决方案是制作[[控制器Alloc] init]并激活一个功能,在我移动到的控制器中做我需要的事情
    【解决方案3】:

    对我来说,解决方案是制作 [[controller alloc]init] 并激活一个函数,在我移动到的控制器中执行我需要的操作

    【讨论】:

      【解决方案4】:

      我已经制作了这个简单的函数来以编程方式在我的 UITabBarController 中选择视图控制器。

      -(void)selectVCfromIndex:(int)index{
      
          self.selectedIndex = index;
          [self tabBarController:self shouldSelectViewController:[self.viewControllers objectAtIndex:index]];
      
      }
      

      在shoudSelectViewController协议中,我拥有特定的代码,用于呈现视图控制器,以这种方式,它运行时标签以编程方式和用户交互改变。

      【讨论】:

        猜你喜欢
        • 2011-06-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-06
        相关资源
        最近更新 更多