简体   繁体   English

如何为每个标签使用使用相同ViewController的UITabBarController?

[英]How do I use a UITabBarController using the same ViewController for every tab?

and change the content and title of the ViewController depending on which tab is selected. 并根据选择的选项卡更改ViewController的内容和标题。

Is it possible? 可能吗?

Below is what I tried within my UITabBarController's viewDidLoad function 以下是我在UITabBarController的viewDidLoad函数中尝试过的方法

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    NSMutableArray * tabs = [[NSMutableArray alloc] init];

    EventsViewController * upcomingEvents = (EventsViewController *)[[self viewControllers] objectAtIndex:0];
    upcomingEvents.title = @"Upcoming Events";
    upcomingEvents.events = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]];

    [tabs addObject:upcomingEvents];

    EventsViewController * myEvents = (EventsViewController *)[[self viewControllers] objectAtIndex:1];
    myEvents.title = @"My Events";
    myEvents.events = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"MyEvents" ofType:@"plist"]];

    [tabs addObject:myEvents];

    [self.tabBarController setViewControllers:tabs animated:YES];

}

This results in the error 这导致错误

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[UINavigationController setEvents:]: unrecognized selector sent to instance 0x108fe3b50'

My current storyboard setup 我当前的故事板设置 我的设定

EDIT: 编辑:

In the end with some help and understand from Unkn0wn.Bit. 最后得到一些帮助,并从Unkn0wn.Bit了解。

I scrapped the custom UITabBarController and just adjusted my viewWillAppear function in my EventsViewController (UITableViewController) to change the source depending on the selectedIndex of the tabBarController. 我废弃了自定义的UITabBarController,并刚刚在我的EventsViewController(UITableViewController)中调整了viewWillAppear函数,以根据tabBarController的selectedIndex更改源。

Effectively using the same view (with different information) for multiple tabBar buttons using two separate NSMutableArrays (myEvents, upcomingEvents). 使用两个单独的NSMutableArray(myEvents,即将发生的事件),对多个tabBar按钮有效地使用相同的视图(具有不同的信息)。

(void)viewWillAppear:(BOOL)animated {
    if([self.tabBarController selectedIndex]) {
        self.events = self.myEvents;
        self.navBar.title = @"My Events";
    } else {
        //temporary instead run function that either downloads events or tries to update if necessary
        self.events = self.upcomingEvents;
        self.navBar.title = @"Upcoming Events";
    }
}

This part of the error message: 错误消息的这一部分:

-[UINavigationController setEvents:] -[UINavigationController setEvents:]

tells you that you are calling setEvents: on an instance of UINavigationController and it doesn't respond. 告诉您您正在UINavigationController的实例上调用setEvents:并且不响应。

Your issue is that you are forgetting your view controller hierarchy because you have your EventsViewController instances inside navigation controllers so you need to traverse the hierarchy to call the correct instances. 您的问题是您忘记了视图控制器层次结构,因为在导航控制器内部有EventsViewController实例,因此您需要遍历层次结构以调用正确的实例。

ie: 即:

UINavigationController *navController = (UINavigationController *)[[self viewControllers] objectAtIndex:0];
EventsViewController *upcomingEvents = [navController topViewController];

如果您使用界面生成器,请在故事板文件中创建多个视图控制器(从Tab Bar Controller中按住ctrl +拖动以将其分配给选项卡),然后根据需要设计视图,但是将其自定义类设置为同一类!

You could use multiple instances of your ViewController subclass, and that would give you what you want. 您可以使用ViewController子类的多个实例,这将为您提供所需的内容。 But if you wanted to simply use the exact same instance for each, then you would want to use a UITabBar directly, not a UITabBarController . 但是,如果您只想为每个实例使用完全相同的实例,那么您将需要直接使用UITabBar而不是UITabBarController You could add the UITabBar to your UIViewController subclass and implement the UITabBarDelegate protocol. 您可以将UITabBar添加到UIViewController子类中,并实现UITabBarDelegate协议。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 UITabBarController在不同选项卡中具有相同的ViewController - UITabBarController with same ViewController in different tab 如何使用 StoryBoard 在 UITabBarController 中设置选定的选项卡? - How do I set selected tab in UITabBarController using StoryBoard? 在UITabBarController上选择选项卡时,如何显示ViewController? - How to present ViewController when tab is selected on UITabBarController? 我可以将UITabBarController用作简单的viewController切换器吗? - Can I use UITabBarController as a simple viewController switcher? 如何在UITabBarController中显示ViewController? - How can I show ViewController in UITabBarController? 如何显示与附加到UITabBarControLler的ViewController不同的ViewController - How could I display different ViewController from ViewController attached to UITabBarControLler 如何将UITabBarController导航到ViewController? - How to navigate UITabBarController to ViewController? UITabBarController应用程序在每个选项卡中都有UIWebViews - UITabBarController app with UIWebViews in every tab UITabBarController添加不带标签栏项目的viewcontroller - UITabBarController add viewcontroller without tab bar item 如何更改UITabBarController的默认ViewController - How to change default ViewController of UITabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM