iOS/xcode/objective-c: How to instantiate view controller after signup - ios

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.

Related

Unable to present a view controller from AppDelegate

I need to present a view controller from app delegate.
When a phone notification comes in, I am able to decide which one of 3 view controllers (named ForumViewController, BlogViewController & NewsViewController) should be presented by analyzing the 'userInfo' in the method 'didReceiveRemoteNotification'.
But when i try to present the appropriate view controller using storyboards or the code below:
self.viewController = [[MembersViewController alloc] initWithNibName:#"MembersViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
Then, the app gives the error 'Warning: Attempt to present whose view is not in the window hierarchy!'. Also it gets stuck on a particular view controller.
Please keep in mind that the view controllers that I am trying to present are not part of the flow when the app starts (the flow is LogoViewController -> SplashViewController -> HomeViewController).
The HomeViewController & MembersViewController are essentially the main menu pages for public & private viewing. Here I have to display something to the viewer.
choice-1
using push
UINavigationController *navController = (UINavigationController *)self.window.rootViewController;
MembersViewController *vc = [navController.storyboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[navController pushViewController:vc animated:YES];
using present
MembersViewController *root = (MembersViewController *)self.window.rootViewController;
UIViewController *vc = [root.storyboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[root presentViewController:vc animated:YES completion:NULL];
upadted
UIStoryboard *mainstoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
MembersViewController* pvc = [mainstoryboard instantiateViewControllerWithIdentifier:#"MembersViewController"];
[self.window.rootViewController presentViewController:pvc animated:YES completion:NULL];
Loading a view controller from the storyboard:
[self performSelector: #selector(ShowModalViewController) withObject: nil afterDelay: 0];
-(void)ShowModalViewController{
NSString * storyboardName = #"MainStoryboard";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"IDENTIFIER_OF_YOUR_VIEWCONTROLLER"];
[self.window.rootViewController presentViewController:vc animated:YES completion:nil];
}
Identifier of your view controller is either equal to the class name of your view controller, or a Storyboard ID that you can assign in the identity inspector of your storyboard.

alloc init does not load component in it

i am using UINavigationController but when i push a view controller just simply by making object of the class the class load but not the components in it.
this is my AppDelegate.m
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewController *vc = [sb instantiateInitialViewController];
UINavigationController *nav = [[UINavigationController alloc] init];
self.window.rootViewController = nav;
nav.viewControllers = #[vc];
and my first UIViewController if action button code is
NextView *next = [[NextView alloc] init];
[self.navigationController pushViewController:next animated:YES ];
it gets to next UIViewController but no internal components are loaded such as - button,textfield,etc.
and if i try this code it give me runtime error
UIStoryboard *sb = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES ];
make sure that NextView inherit UIViewController and you have given identifier as two to that view controller and you have set class NextView from identity inspector to that viewcontroller.
Hope this will help :)
You could not get outlets (buttons etc.) to be loaded with alloc init in the subclass of UIViewController. You should init it with the xib file, using -initWithNibName:bundle: method. Or you can instantiate it with storyboard.
// You can get current storyboard from the View Controller
UIStoryboard *sb = vc.storyboard;
NextView *next = [sb instantiateViewControllerWithIdentifier:#"two"];
[self.navigationController pushViewController:next animated:YES];

Navigation and Tab Bar Controller not showing up

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;

NavigationController Header Disappears when loading from AppDelegate

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.

ios adding navigation before splitview controller

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.

Resources