【问题标题】:Can I make a UITabBarController switch tabs programmatically?我可以以编程方式制作 UITabBarController 切换选项卡吗?
【发布时间】:2013-07-16 00:37:58
【问题描述】:

我有一个基于 UITabBarController 模板的应用程序,其中第一个选项卡是 UITableViewController,单元格中有自定义按钮。我需要一个单元格中的一个按钮来调用父 UITabBarController 中的特定选项卡。我在 didSelectRowAtIndexPath 中使用此代码:

case 5:
            NSLog(@"Search");
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
            // Get UITabBar instance and send it message
            UITabBarController *tabBar = (UITabBarController*)[self parentViewController];
            [tabBar setSelectedIndex:1];

            break;

当我点击此行时,应用程序在控制台中没有任何日志的情况下崩溃。我在 UITableViewController 中调用它,它是 UITabBarController 中的第一个选项卡。

PS 我还想避免在用户点击时出现在此单元格上的蓝色选择。所以我添加了这段代码:

- (NSIndexPath *)tableView:(UITableView *)tableView willSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.row) {
        case 1:
            return nil;
            break;
    }
}

但是当我点击它时它仍然选择蓝色。它们可能是相关的问题吗?

【问题讨论】:

  • 你的意思是像this
  • 您可以通过为单元格设置选择样式UITableViewCellSelectionStyleNone 来避免蓝色选择。 case 5didSelectRowAtIndexPath中指的是什么?

标签: ios uitabbarcontroller


【解决方案1】:
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    UITabBarController *tabController = (UITabBarController *) UIApplication.sharedApplication.keyWindow.rootViewController;
    [tabController setSelectedIndex:1];
}

第一条消息将取消选择该行,避免蓝色选择。

由于您的应用是基于选项卡的,因此键窗口的根控制器始终是UITabBarController 的实例。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-07
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2011-07-21
    相关资源
    最近更新 更多