Jebzaki
Jebzaki

Reputation: 163

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 My 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";
    }
}

Upvotes: 0

Views: 1416

Answers (3)

Wain
Wain

Reputation: 119021

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.

i.e.:

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

Upvotes: 3

Gavin
Gavin

Reputation: 8200

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.

Upvotes: 1

Sepehrom
Sepehrom

Reputation: 1335

If you use interface builder, In your story board file create multiple view controllers ( ctrl+drag from your Tab Bar Controller to assign them to tabs ) design your view as you wish, but set their custom class to one same class !

Upvotes: 1

Related Questions