I want to add a navigation view controller prior to the user getting to the splitview controller. I have tried a few ways of changing the root controller when I want to go from navigation controller to splitview controller but I don't seem to be setting the delegate the right way when I do this.
Code WITHOUT nav view (works perfectly):
AppDelegate
UISplitViewController *splitViewController = (UISplitViewController *)self.window.rootViewController;
UINavigationController *navigationController = [splitViewController.viewControllers lastObject];
splitViewController.delegate = (id)navigationController.topViewController;
UINavigationController *masterNavigationController = splitViewController.viewControllers[0];
MasterViewController *controller = (MasterViewController *)masterNavigationController.topViewController;
controller.managedObjectContext = self.managedObjectContext;
Code with nav view prior to SplitView
AppDelegate
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"dummy"];
self.window.rootViewController = rootController;
[self.window makeKeyAndVisible];
DummyViewController
AppDelegate *appDelegateTemp = [[UIApplication sharedApplication]delegate];
appDelegateTemp.window.rootViewController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateInitialViewController];
This takes me from the DummyViewController that I launched into, to the splitview controller which is the initial view controller in Storyboard. Which is fine however, when I do it this way none of the delegates get called. This is probably because when changing root controllers, it is not setting the delegates properly. How can I get this to work the right way?
It seems the only really non-hacking way to do it is to present a modal view over the split view in the detail view controller
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
DummyViewController *dummy = (DummyViewController *)[storyboard instantiateViewControllerWithIdentifier:#"dummy"];
[self presentViewController:dummy animated:NO completion:nil];
By setting animation to NO, the user does not see the split view loaded behind it.
Related
I'm trying to use instantiateViewControllerWithIdentifier in the app delegate to show a view controller but for some reason the navigation bar and tab bar isn't show up. I'm not sure what I'm doing wrong - Thanks
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *view = (UINavigationController *)[sb instantiateViewControllerWithIdentifier:#"ShopViewController"];
self.window.rootViewController = view;
Is ShopViewController a UINavigation controller or is it just a view controller. It sounds like you are declaring the view controller as uinavigationcontroller.
Instead you should either drop a navigation controller on the storyboard and then give that an identifier OR you could just create a navigation controller in the app delegate.
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ShopViewController *showViewController = (ShopViewController *)[sb instantiateViewControllerWithIdentifier:#"ShopViewController"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:shopViewController];
self.window.rootViewController = nav;
After a successful sign up, I want users to go through a process where they provide photo etc. Similar code to that below worked to load the login page but code below is not working to load a separate view controller in storyboard "newuser".
Would appreciate any suggestions on how to fix.
- (void)presentNewUserInterface
{
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"newuser"];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window.rootViewController = navigation;
}
Instead of:
- (void)presentNewUserInterface
{
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"newuser"];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:rootController];
self.window.rootViewController = navigation;
}
Try:
- (void)presentNewUserInterface
{
UIViewController* rootController = [[UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]] instantiateViewControllerWithIdentifier:#"newuser"];
UINavigationController* navigation = [[UINavigationController alloc] initWithRootViewController:rootController];
[self presentViewController:navigation animated:YES completion:nil];
}
You aren't presenting your new navigation controller in your example. Is there a particular reason why you would be trying to replace your rootViewController? That shouldn't be needed. My example should be sufficient for displaying your next flow in the user sign up process.
Two variants:
Replace window.rootViewController with your next view
controller. Do it on AppDelegate class.
Present new view controller
from current root view controller. Do it in your sign up view
controller.
The lack of the first method is no transaction animation. So second method is preferable.
In the AppDelegate i check if there is a user existing, if there is i "skip" two ViewControllers by pushing a NavigationController using this code:
if(currentUser){
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UIViewController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"Test"];
self.window.rootViewController = viewController;
viewController.navigationController.navigationBarHidden = NO; // Tried this nothing happened.
[self.window makeKeyAndVisible];
}
else{
[PFUser enableAutomaticUser];
[[PFUser currentUser] incrementKey:#"RunCount"];
[[PFUser currentUser] saveInBackground];
[[PFUser currentUser] fetch];
self.window = [[UIWindow alloc] initWithFrame:UIScreen.mainScreen.bounds];
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *viewController = [storyboard instantiateViewControllerWithIdentifier:#"firstView"];
self.window.rootViewController = viewController;
[self.window makeKeyAndVisible];
}
So the true statement skips two ViewControllers and push the UINavigationController. But when it shows it this way there is no top bar.
However if there was no user then it would proceed to the UINavigationController in a "normal" way by segues then the bar is visible.
How can i fix this?
Thank you!
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main"
bundle:nil];
UIViewController *viewController = [storyboard
instantiateViewControllerWithIdentifier:#"Test"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:viewController];
self.window.rootViewController = nav;
"So the true statement skips two ViewControllers and push the UINavigationController." No, it doesn't. First of all you're not pushing anything, you're setting the window's root view controller. Secondly, you haven't created any navigation controller, you've only created viewController.
If viewController has a navigation controller in the storyboard, then you should instantiate the navigation controller (which will, in turn, instantiate viewController), and make it the window's root view controller.
I think you should set a UINavigationController to rootviewController , there will be a navigation bar. So you can also set the firstView as rootViewController, but set the viewcontrollers attribute to put the Test viewcontroller that clears before viewcontrollers.
In my AppDelegates 'didFinishLaunchingWithOptions' function, I have this code in there:
if(loggedIn != nil)
{
MainViewController *mvc = [self.window.rootViewController.storyboard instantiateViewControllerWithIdentifier:#"MainView"];
[self.window setRootViewController:mvc];
}
Second Attempt which didn't work:
if(loggedIn != nil)
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MainViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"MainView"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
}
The problem is that when the MainViewController loads, the NavigationHeader is missing. I've tried various methods online and instantiations that basically do the same thing to no avail. I have also tried created a whole new navigationController and adding my view to it, however, that fails as well.
Your setting MainViewController as your root, if this is not a navigation controller, there will be no header when it opens.
Instead create a UINavigationController, set MainViewController as its root and then set the navigation controller as the window root.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
UIViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"home"];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[self.window setRootViewController:nav];
or if you have the navigation controller inside the storyboard then instantiate that. Most likely the initial view controller.
e.g.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyboardName" bundle:nil];
[self.window setRootViewController::[storyboard instantiateInitialViewController]];
not sure if it will be the initial viewController or not, that requires more info of your setup to know.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UINavigationController *navVC = [UINavigationController new];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
[navVC setViewControllers:[NSArray arrayWithObject:VC1] animated:NO];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
}
Now this creates a navigationController but when i try to display the secondViewController it does display me the correct Navigation Bar for that view but i see a black background instead of the actual view. Adding secondViewController as a subview works but when i now try to display the third one it does again display me the correct navigation bar with the set title for this view but i still see the secondViewController's view. Now my question how do I add viewController to the navigationController so they are displayed right? do i even have to add them? I've read through the apple class reference but there isn't any code.
try this:
self. window = [[UIWindow alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *VC1 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC"];
UINavigationController *navVC = [[UINavigationController alloc] initWithRootViewController:VC1];
[self. window setRootViewController:navVC];
[self. window makeKeyAndVisible];
return YES;
now inside VC1 push the next view controller to your stack with
// EDIT:
UIStoryboard *mStoryboard = [UIStoryboard storyboardWithName:#"your storyboard name" bundle:nil];
ViewController *vC2 = [mStoryboard instantiateViewControllerWithIdentifier:#"VC2"];
[self.navigationController pushViewController:vC2 animated:YES];