简体   繁体   中英

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.

Is it possible?

Below is what I tried within my UITabBarController's viewDidLoad function

- (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.

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.

Effectively using the same view (with different information) for multiple tabBar buttons using two separate NSMutableArrays (myEvents, upcomingEvents).

(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:]

tells you that you are calling setEvents: on an instance of UINavigationController and it doesn't respond.

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.

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. 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 . You could add the UITabBar to your UIViewController subclass and implement the UITabBarDelegate protocol.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM