From initial View Controller how to segue acriss to a UITabBarController and use prepareForSegue

719 views Asked by At

Here is the setup:

1) Single View Controller - inital Cobntroller = with a button.

2) Tab Bar Controller controller with 1 View Controller - Label

When I press the button it will segue over to the Tab View.

Now I want to use the prepareForSegue to pass over a String to update the label of the 1 View Controller inside the Tab Bar Controller.

When I do this I get a

"unrecognized selector sent to instance".

So I cab segue to the Tab View Controller.

However I am not able to reference the ViewController inside the Tab.

How can I fix this ?

1

There are 1 answers

6
Aaron Bratcher On BEST ANSWER

The problem is that you cannot segue from a single view to a TabBarController. Instead, use code like this:

- (IBAction)setupTapped:(id)sender {
    static NSString *tabViewControllerIdentifier = @"SetupViewController";  


    UIViewController *controller = [self.storyboard instantiateViewControllerWithIdentifier:tabViewControllerIdentifier];
    AppDelegate *app = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    UIViewController *currentController = app.window.rootViewController;
    app.window.rootViewController = controller;
    app.window.rootViewController = currentController;

    [UIView transitionWithView:self.navigationController.view.window
                      duration:0.75
                       options:UIViewAnimationOptionTransitionFlipFromRight
                    animations:^{
                        app.window.rootViewController = controller;
                    }
                    completion:nil];
}

That will transition between controllers. You can change the animation type to suit your purpose. This is set with the options parameter; currently showing UIViewAnimationOptionTransitionFlipFromRight.

"SetupViewController" is the Storyboard ID (without the quotes) entered into the identity inspector (3rd icon from the right) for the TabViewController. You can change this to match what you have entered.