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.
-
Is the first view controller a splash screen or something to welcome the user and then the SplitView is the main part of the application? – Walter Apr 23 '14 at 17:24
-
no its not splash screen. Its login screen. once user click on login button it should go next view i.e. split view. – nilkash Apr 23 '14 at 17:30
-
I don't think you are creating an iPhone app with a `UISplitViewController`. An iPad app, yes. But not an iPhone app. – rmaddy Apr 23 '14 at 17:41
-
:) yeah actually both iphone and ipad – nilkash Apr 23 '14 at 17:44
-
[Here](http://stackoverflow.com/questions/4213097/best-way-to-switch-between-uisplitviewcontroller-and-other-view-controllers/25979945#25979945) is how I handled this kind of situation, hope that helps. – tadija Sep 24 '14 at 08:56
1 Answers
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;

- 5,867
- 2
- 30
- 43