Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to go to a specific tab in a UITabBarController from a view controller

First of all, let me say I'm an absolute beginner, so please forgive me if this is a stupid questions to be asking. And let me just add that I've spent hours/days trying to figure out how to solve this problem - including extensive searches on stackoverflow (maybe the answer is somewhere and I just don't now exactly what to search for :)...

But, let's continue: I have a small (?) problem with an Xcode storyboard project. Basically my project looks like this:

Navigation Controller -> View Controller 0 -> Tab Bar Controller -< View Controller 1, View Controller 2, View Controller 3.

When the user pushes 'button #2' in the View Controller 0, I'd like him/her to jump directly to 'View Controller 2'.

Would that be possible at all, and if so what code should use and excatly where should I put it.

Hope someone out there will help a newbie out :)

Regards, Ulrik

like image 960
ulrikbuchert Avatar asked Mar 24 '14 18:03

ulrikbuchert


People also ask

How do I add a tab to my existing view controller?

Go to Editor > Embed In > Navigation Controller, and then straight away go to Editor > Embed In > Tab Bar Controller. The navigation controller adds a gray bar at the top called a navigation bar, and the tab bar controller adds a gray bar at the bottom called a tab bar.

What is a tab bar view controller?

A tab bar controller is a powerful UI component for iOS apps. It's a container view, and you use it to group view controllers together. They give your app's user access to the most important screens of your app.


1 Answers

Yes it is possible. You may show any view controller from any other. You should simply add a segue from button #2 to View Controller 2. (I assume you have all your controllers in single storyboard)

Update: the above solution will show you View Controller 2 itself without tab bar controller.

Hard to tell in details without seeing the actual code. For more details you may refer to these documents:

  • View Controller Basics (especially part "Storyboards Help You Design Your User Interface")
  • Presenting View Controllers from Other View Controllers
  • Using View Controllers in Your App

Probably you'll come up with more concrete question.

Update If you want to preselect desired view controller inside tabbar controller you may use the following code sketch. Here you can programmatically initiate a segue and do the desired pre-initialization inside prepareForSegue:sender: method.

static NSString * const kShowTabSegueID = @"ShowTab";

@interface ViewController ()

- (IBAction)buttonOnePressed;
- (IBAction)buttonTwoPressed;
- (IBAction)buttonThreePressed;

@end

@implementation ViewController

- (IBAction)buttonOnePressed
{
    [self performSegueWithIdentifier:kShowTabSegueID
                              sender:@0];
}

- (IBAction)buttonTwoPressed
{
    [self performSegueWithIdentifier:kShowTabSegueID
                              sender:@1];
}

- (IBAction)buttonThreePressed
{
    [self performSegueWithIdentifier:kShowTabSegueID
                              sender:@2];
}
- (void)prepareForSegue:(UIStoryboardSegue *)segue
                 sender:(id)sender
{
    if ([segue.identifier isEqual:kShowTabSegueID]) {
        NSNumber *indexToShow = sender;
        UITabBarController *tabBar = segue.destinationViewController;
        [tabBar setSelectedIndex:indexToShow.unsignedIntegerValue];
    }
}

@end
like image 52
Max Komarychev Avatar answered Oct 22 '22 04:10

Max Komarychev