nilkash
nilkash

Reputation: 7536

Set split view controller as root view controller dynamically IOS

Hi I am developing Iphone application in which I am using split view controller. Apple doc clearly says that split view controller must be at root position but in my condition it's not at root position. So I want to set it as root dynamically. Initially I tried to set it in app delegate but it wont work. My application is like this; it has first one UI view controller which have one button. On button click I am moving on split view controller. That mean it's not at root position. I tried to to change root view controller on button click action but it's not working. Is there have properly example for this. I am using story board. Need help. Thank you.

Upvotes: 0

Views: 2071

Answers (1)

Walter
Walter

Reputation: 5887

Make the UISplitViewController the rootView of the storyboard, just like Apple says. Then add the login View Controller as a modal and present it (probably without animation) from the split view controller.

Here is how I am doing something similar in one of my apps.

In applicationDidFinishLaunching:withOptions: I am taking Apple's code directly. For the iPad they set a splitViewController as the root if you are using the Master/Detail template.

Then in applicationWillEnterForeground: I display my login screen.

-(void)applicationWillEnterForeground:(UIApplication*)application {
       UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;


       UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:@"Main_iPad" bundle:nil];
       UIViewController *myViewController = [mainStoryboard  instantiateViewControllerWithIdentifier:@"whatever name I gave my VC as its Storyboard ID"];

       [splitViewController presentViewController:myLoginViewController animated:NO completion:nil];
 }

I use application will enter foreground so that the login screen will popup on app launch and when they come back from the background. You might want to do it differently.

Also as others mentioned, since your app is universal you only want to reference the splitViewController when you are on an iPad so put an if block around all of this. If you also want to present a login for the iPhone, it will be slightly different, you'll use the iPhone storyboard and you'll present from the iPhone root. In the Apple Master/Detail template that root is

 UINavigationController *navigationController = (UINavigationController *)self.window.rootViewController;

Upvotes: 1

Related Questions