【问题标题】:How to toggle view controllers using a UITabBar?如何使用 UITabBar 切换视图控制器?
【发布时间】:2022-01-02 19:12:38
【问题描述】:

我对这个项目的目标是在使用 UITabBar 的视图控制器之间循环,而不使用 UITabBarController,因为根据 Apple 文档,不应将 TabBarControllers 推送到该项目已经使用的 UINavigationControllers。

到目前为止,我使用的是@samuel 对此问题的回答中的UITabBarHow to add UITabBar in iphone using objective c

//viewDidLoad
...  

UITabBar *tabBar = [[UITabBar alloc] initWithFrame:CGRectMake(0, 431, 320, 50)];
[self.view addSubview:tabBar];
[[UITabBar appearance] setBarTintColor:[UIColor blackColor]];

NSMutableArray *tabBarItems = [[NSMutableArray alloc] init];
UITabBarItem *tabBarItem = [[UITabBarItem alloc] initWithTitle:@"Home" image:[UIImage imageNamed:@""] tag:0];
UITabBarItem *tabBarItem1 = [[UITabBarItem alloc] initWithTitle:@"Rules" image:[UIImage imageNamed:@""] tag:1];
[tabBarItems addObject:tabBarItem];
[tabBarItems addObject:tabBarItem1];
tabBar.items = tabBarItems;
tabBar.selectedItem = [tabBarItems objectAtIndex:0];
}

当我按下 tabBarItems 时,我已准备好在视图控制器之间循环。这样做的正确方法是什么?我想在点击Rules tabBarItem 时转到名为ViewController9 的视图控制器,然后在点击Home tabBarItem 时转到ViewController6

有人可以分享一些代码吗?这是我尝试过的,但按下 tabBarItems 时没有任何反应:

- (void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item {
NSInteger selectedTag = tabBar.selectedItem.tag;
NSLog(@"%ld",(long)selectedTag);
if (selectedTag == 0) {
    //Do what ever you want here
    NSString * storyboardName = @"MainStoryboard";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController9"];
    [self presentViewController:vc animated:YES completion:nil];
} else if(selectedTag == 1) {
    NSString * storyboardName = @"MainStoryboard";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"ViewController9"];
    [self presentViewController:vc animated:YES completion:nil];
} else { //if(selectedTag == 2)
    //Do what ever you want here
}
}

【问题讨论】:

    标签: ios objective-c


    【解决方案1】:

    如何使用 Objective C 的 UITabBar 推送视图控制器?

    如果你仔细想想,这个问题没有多大意义。 “推送”是一个堆栈操作,一个导航控制器管理着一堆视图控制器。 UITabBar 根本不是一个视图控制器,它不管理任何东西的堆栈,所以推送不是一个可用的操作。

    我对这个项目的目标是在使用 UITabBar 的视图控制器之间循环,而不使用 UITabBarController,因为根据 Apple 文档,不应将 TabBarControllers 推送到该项目已经使用的 UINavigationControllers。

    这是有原因的,原因是导航控制器和选项卡控制器提供了不同的模式来控制用户在屏幕上看到的内容。如果选项卡控制器可以是导航堆栈的一部分,则用户会看到选项卡栏出现和消失,而它本来是固定的。它会创建一个非常混乱的界面。

    如果您单独使用标签栏来解决 Cocoa Touch 不鼓励您创建令人困惑的用户界面这一事实,那么您最终会得到一个令人困惑的用户界面。仔细想想你真正想要做什么,如果你决定仍然需要这样做,请考虑使用看起来不像标签栏的 UI 元素,这样用户就不会期待标签栏的行为。

    【讨论】:

    • 我编辑了问题的标题以减少混淆。看起来你是对的。您将如何实现相关功能?不是 UITabBar 仍然是一种选择吗?
    • 该项目有一个注册和登录过程,所以我不想在每个视图控制器上显示一个 tabBarController。只是登录后显示的视图控制器。我正在尝试显示一个新的视图控制器,用户需要查看它才能了解如何使用该应用程序,我认为 UITabBar 将有助于实现这一目标。
    • @programcode 你知道你可以改变根视图控制器,对吧?因此,如果需要,请显示您的登录界面,然后在用户登录时切换根视图控制器。如果他们注销,则切换回来。
    • 我是说如果你想要一个基于标签的界面,你可以使用一个标签栏控制器……只要让它成为根视图控制器。
    • 现有的代码问题很多……试试这个:stackoverflow.com/q/15774003
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-26
    • 2011-01-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多