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.
Related
I have a situation wherein a storyboard made VC(embedded in NavController) should be presented programmatically.
SomeVC -> presents NavController(rootVC) -> rootVC -> pushes subVC
on this representation, subVC should have a back button to go back to rootVC, but I can't implement it this way. Will be providing sample codes that I have already tried.
this pushes the rootVC directly:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"storyBoard" bundle: nil];
UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:#"rootVC"];
[self.navigationController presentViewController:vc animated:YES completion:nil];
also tried pushing the navigationController itself, ID has been set on storyboard:
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
[self presentViewController:navController animated:YES completion:nil];
EDIT: storyboard implementation looks like this
I'm not sure if I understand you correctly. You want to present a navigation controller and have those rootVC and subVC already in it, right? So after presenting, you want the subVC to be presented with the back button right away.
If that's the case you need to tell the navigation controller to push the subVC before presenting it
UINavigationController *navController = [storyboard instantiateViewControllerWithIdentifier:#"navigationController"];
UIViewController *subVC = [storyboard instantiateViewControllerWithIdentifier:#"subVC"];
[navController pushViewController:subVC animated:NO];
[self.navigationController presentViewController:vc animated:YES completion:nil];
Hi i have two Viewcontroller A and B and i am presenting B Controller from A Controller with Current Context and I pasted my coding below. All i want to push from B controller to C controller, I know that no navigation controller allocated when we present and if i present navigationcontroller with rootviewcontroller i cannot achieve transparent result.
UIStoryboard *story = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ChooseAddressVc *sec=[story instantiateViewControllerWithIdentifier:#"ChooseAddressVc"];
sec.myDelegate = self;
sec.modalPresentationStyle = UIModalPresentationOverCurrentContext;
sec.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:sec animated:YES completion:^{
}];
Pushing from Viewcontroller B to C
UIStoryboard*Story=[UIStoryboard storyboardWithName:#"Main2" bundle:nil];
AddNewAddressVc*choose=[Story instantiateViewControllerWithIdentifier:#"AddNewAddressVc"];
[self.navigationController pushViewController:choose animated:YES];
Note:I need to push from B to C controller when i present with current context. FOR BETTER UNDERSTANDING:WE CANNOT ALLOCATE NAVIGATION CONTROLLER AS ROOT VIEWCONTROLLER FOR A WHEN WE NEED TO GET TRANSPARENCY EFFECT
In your case self.navigationController in B view controller is nil. You need to create UINavigationController with B view controller as rootViewController. Present created UINavigationController from A instead presenting B view controller, after this you're able to use self.navigationController in B view controller.
Your code edited:
UIStoryboard *story = [UIStoryboard storyboardWithName:#"Main" bundle:[NSBundle mainBundle]];
ChooseAddressVc *sec=[story instantiateViewControllerWithIdentifier:#"ChooseAddressVc"];
sec.myDelegate = self;
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:sec];
navController.modalPresentationStyle = UIModalPresentationOverCurrentContext;
navController.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
[self presentViewController:navController animated:YES completion:^{
}];
I have 5 view controller and want to come back from the last view to 1st view controller
Call a function in which you have to change the app rootviewcontroller. set it as a navigationController
yourViewcontoller *viewController = [[yourViewContoller alloc] init];
UINavigationController *navController=[[UINavigationController alloc]initWithRootViewController:viewController];
self.window.rootViewController = navController;
If you're using storyboards, give the UINavigationController a Storyboard ID. You can then initiate it and present through the code:
UINavigationController *vc = [self.storyboard instantiateViewControllerWithIdentifier:#"<Storyboard ID>"];
[self presentViewController:vc animated:YES completion:nil];
Use this method on Animation splash screen
[self performSelector:#selector(setHidden:) withObject:nil afterDelay:2.0];
-(void) setHidden:(id)sender
{
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboare" bundle:nil];
YourVC* vController = (YourVC*)[Storyboard instantiateViewControllerWithIdentifier:#"storyboardId"];
UINavigationController *navcotoller=[[UINavigationController alloc]initWithRootViewController:vController];
self.window.rootViewController =navcotoller;
}
If you are using UINavigationController you could and should simply call
[self.navigationController popToRootViewControllerAnimated:YES];
I have a certain UINavigationController in storyboard I present modally from another view controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:#"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
This navigation controller has set another UIViewController as its root view controller in storyboard. I'd like to set some properties for this root view controller before it is shown, but I tried this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:#"myNavController"];
[self presentViewController:myNavController animated:YES completion:nil];
MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:#"myRootViewController"];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
But the root view controller doesn't seem to be loaded yet when I try to set its properties...
How could I do this? Thanks
If you had debugged, you will find the myRootViewController made by MyRootViewController *myRootViewController = [storyboard instantiateViewControllerWithIdentifier:#"myRootViewController"];
isn't the myNavController's real rootViewController.
This method -instantiateViewControllerWithIdentifier: just give you a whole new UIViewController instance.
So, If you want get the real rootViewController, just replace it with following:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
UINavigationController *myNavController = [storyboard instantiateViewControllerWithIdentifier:#"myNavController"];
MyRootViewController *myRootViewController = myNavController.viewControllers[0];
[myRootViewController setSelectedItem:selectedItem];
[myRootViewController setDelegate:self];
[self presentViewController:myNavController animated:YES completion:nil];
I am trying the "pass the baton" method for passing the managedObjectContext (MOC) thru multiple views. I have it successfully passed to the rootViewController. From there I move to a tabBarController via presentViewController. I can't seem to find a way to pass the MOC when the tabBarController is pushed.
AppDelegate.m
UIViewController *navigationController = (UIViewController *)self.window.rootViewController;
MyViewController *controller = (MyViewController *) navigationController;
controller.managedObjectContext = managedObjectStore.mainQueueManagedObjectContext;
The main view controller is basically a start up screen that will kick you into a login screen or if you are already logged in, to the tabBarController. Below is where I transition to the tabBarController from within the viewDidAppear method.
MyViewController.m
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];
The tabBarController in the storyboard has the identifier "mainTabVC".
I've tried lines like
MyTabBarController.managedObjectContext = self.managedObjectContext;
but I get the error Property 'MOC' not found on object of type MyTabBarController even though I do have the property declared in MyTabBarController.h
Could someone show me a line of code that I can throw in this segue to push the MOC to the tab bar controller.
BTW- I'm utilizing RestKit in this app if that changes the way I should be handling this please let me know.
*****Solution********
To make things clear for any other new guys with the same question. I went from this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
UIViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];
To this:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"MainStoryboard_iPhone" bundle:nil];
MyTabBarViewController *mainTabVC = [storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
mainTabVC.managedObjectContext = self.managedObjectContext;
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];
Notice the assignment in the third line and using MyTabBarViewController instead of UIViewController in the second line. BIG thanks again to rdelmar!
Your code is somewhat confusing. Is MyTabBarController the class? It looks like mainTabVC is your instance. You should use that rather than the class, and you should change the type when you instantiate mainTabVC to MyTabBarController, instead of UITabBarController. You also don't need to get the storyboard the way you do, you can just use self.storyboard.
MyTabBarController *mainTabVC = [self.storyboard instantiateViewControllerWithIdentifier:#"mainTabVC"];
mainTabVC.managedObjectContext = self.managedObjectContext;
[mainTabVC setModalPresentationStyle:UIModalPresentationFullScreen];
[self presentViewController:mainTabVC animated:NO completion:nil];