jeddi
jeddi

Reputation: 651

Swapping views in a tab bar app calling the view controller directly and not by its index

I want to swap views in a tab bar application but I can't rely on the index of the view controllers as they change. So I cannot use the code below:

    [self.tabBarController setSelectedIndex:4];

I want to be able to call a specific view controller by name. How can I do this please?

Thanks

Upvotes: 0

Views: 78

Answers (1)

robbartoszewski
robbartoszewski

Reputation: 896

You can select view controller of a specific class using this code:

NSUInteger index = [self.tabBarController.viewControllers indexOfObjectPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop) {
                    return [obj isKindOfClass:[ClassOfViewControllerYouAreLookingFor class]];
            }];
if(index != NSNotFound) {
    [self.tabBarController setSelectedIndex:index];
}

Or you can simply select a specific ViewController instance using this code:

NSUInteger index = [self.tabBarController.viewControllers indexOfObject:yourViewController];
if(index != NSNotFound) {
    [self.tabBarController setSelectedIndex:index];
 }

Upvotes: 2

Related Questions