Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to segue a view controller on app load using tabbarcontroller

I am using a tabbarcontroller as the root view controller. Unfortunately, using the new storyboard functionality, it is proving difficult to segue a view controller - Login Page - on the app load.

I am using the below code:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    UITabBarController *tabBarController = (UITabBarController *)self.window.rootViewController;

    [tabBarController performSegueWithIdentifier:@"loginPage" sender:self];

The segue is set up properly. I went into one of the tabs view controllers and made an IBAction and it successfully segued. Thanks in advance.

like image 771
mark harding Avatar asked Dec 27 '22 11:12

mark harding


2 Answers

Ran into this same issue today. I had to call:

[self.window makeKeyAndVisible];

before

[self.window.rootViewController performSegueWithIdentifier:@"LoginView" sender:self];

So I'm assuming that when using storyboards the makeKeyAndVisible happens after didFinishLaunchinWithOptions: returns. So when were calling the segue its happening on a view thats not onscreen.

like image 76
Jacob Rush Avatar answered May 16 '23 09:05

Jacob Rush


I ran recently into the same issue. However, the solution provided did not work out for me.

The reason was that I used a "push" segue to display my login view controller (which was embedded inside a navigation controller). Changing the style of the segue from "push" to "modal" did the trick for me. Apparently, it is not possible to initiate a "push" segue from within a tab bar controller but only from within a navigation controller.

Furthermore, I did not put the line

[self performSegueWithIdentifier:@"LoginSegue sender:self];

in the method didFinishLaunchingWithOptions:didFinishLaunchingWithOptions: of the app's delegate, but rather in the method viewDidAppear:. Doing so, I did not need the following line of code:

[self.window makeKeyAndVisible];

Hope this is useful to others.

like image 40
Tomasz Nguyen Avatar answered May 16 '23 07:05

Tomasz Nguyen