【问题标题】:How to stay in the same tab when poping off from Navigation Controller从导航控制器弹出时如何保持在同一个选项卡中
【发布时间】:2013-01-04 07:47:23
【问题描述】:

我的意图是创建一个应用程序,该应用程序具有可单击的 UIButton 的主视图。单击其中一个按钮将进入 TabBarController 视图。

例如,当您单击 sample1 按钮时,将自动选择 TabBarController 视图中的第二个选项卡。当您单击 sample2 按钮将转到第三个选项卡时,更多按钮将转到更多选项卡,该选项卡将显示表格视图。

我的问题是当我单击 MoreViewController 中的 UITableViewCell 进入详细视图时。但是,当我想通过单击左上角按钮返回 TabBarController 时。该选项卡将更改为我在主视图中单击的选项卡。

TabBarController 的 selectedIndex 似乎没有改变,即使我在委托方法中设置了值

- (void)tableView:(UITableView *)tableView 
didSelectRowAtIndexPath:(NSIndexPath *)indexPath

或MoreViewController中的viewWillAppearviewWillDisappear等其他地方,selectedIndex的值仍然保持不变。

示例代码在这里 -> http://uploadingit.com/file/qxfbw4cuzjdkk56v/9s.zip

有人知道如何解决这个问题吗?

【问题讨论】:

    标签: ios uitableview uibutton tabbarcontroller poptoviewcontroller


    【解决方案1】:

    实际上,您正在使用主导航控制器从主视图推送您的标签栏控制器。因此,它会在弹出 POP 时将您带到主视图。

    试试这个来创建标签栏

    tabBar_Controller = [[UITabBarController alloc] init];
    NSMutableArray *localControllersArray =[[NSMutableArray alloc]initWithCapacity:2];
    
    
    firstViewController = [[FirstViewController alloc] initWithNibName:@"firstViewController" bundle:nil];
    nav = [[UINavigationController alloc] initWithRootViewController:firstViewController];
    nav.tabBarItem.title = @"item1";
    nav.navigationBar.barStyle = UIBarStyleBlack;
    [localControllersArray addObject:nav];
    [self setNav:nil];
    
    secondViewController = [[SecondViewController alloc] initWithNibName:@"secondViewController" bundle:nil];
    nav = [[UINavigationController alloc]initWithRootViewController:secondViewController];
    nav.tabBarItem.title = @"item2";
    [localControllersArray addObject:nav];
    [self setNav:nil];
    
    tabBar_Controller.viewControllers = localControllersArray;
    tabBar_Controller.delegate = self;
    tabBar_Controller.selectedIndex = 0;
    [self.window addSubview:tabBar_Controller.view];
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-08-02
      • 1970-01-01
      • 1970-01-01
      • 2019-05-28
      • 2020-10-04
      • 2012-01-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多