There are similar questions but they are either in swift or are not solving my problem.I've a view controller which is presenting navigation view controller when cell did select button is pressed as:
patientBillNavigationViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:#"PatientBillVC"];
//soem value assignment
[self presentViewController:viewController animated:YES completion:nil];
You can say billing opens up a whole new view independent of the main app flow to handle billing process.
The view this navigation View Controller automatically loads is the bill view and now if I want to pass a value from this viewcontroller to the other viewController embedded in navigation view I can't do that. How to pass a value?
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle: nil];
PatientBillViewController *vc = [storyboard instantiateViewControllerWithIdentifier:#"PBVC"];
vc.superBillToAddEdit = sb;
//then display the view embedded in navigation view
But this is not working.
One way I know is to Subclass UINavigationViewController and in its viewDidLoad, you can do:
YourEmbeddedVc *svc =self.viewControllers[0]; //get the controller reference
svc.name= #"Hello";
I'm assuming you are only having one controller in your navigation stack, if not you need to find out the proper match of the required VC.
I might not be understanding the question exactly, correct me if I'm wrong.
It sounds like you want to start a nav controller from a view controller (let's call this firstViewController) and pass a value from firstViewController to the view controller that will eventually load in the nav controller.
If that's the case, why don't you create the second view controller (billingStep1ViewController) and then assign the value to it as a property, then pass the billingStep1ViewController to the nav controller's initWithRootViewController: method?
Something like this (untested code, btw):
// this code would go inside our FirstViewController file
// create the first vc that will be loaded in the nav controller
BillingStep1ViewController *billingStepOneVc = [self.storyboard instantiateViewControllerWithIdentifier:#"stepOne"];
// set the value you want
billingStepOneVc.bill = myBill;
// create new nav controller with step one vc as the root
UINavigationController *uiNavController = [[UINavigationController alloc] initWithRootViewController:billingStepOneVc];
[self presentViewController:uiNavController];
That way you can have two view controllers talking directly with one another and not worry about the navigation controller at all.
Update: Here is some tested code that works to illustrate my idea:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//create the step 1 view controller - this is the first view controller we will see in the navigation controller
StepOneViewController *stepOneVc = [storyboard instantiateViewControllerWithIdentifier:#"stepOne"];
//assign a value to it (name is an NSString #property of StepOneViewController)
stepOneVc.name = #"Jones";
//this is the nav controller we will display, we set the root vc to our step one.
UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:stepOneVc];
//present the nav controller on screen.
[self presentViewController:navController animated:YES completion:nil];
Some of the class names changed but the idea is the same.
Rather than creating the navigation controller from its storyboard identifier, create it programmatically, with the PatientBillViewController you created as its root view controller. Like this:
UIStoryboard *mainStoryboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
PatientBillViewController *pbVC = [mainStoryboard instantiateViewControllerWithIdentifier:#"PBVC"];
pbVC.superBillToAddEdit = sb;
PatientBillNavigationViewController * navVC = [[PatientBillNavigationViewController alloc] initWithRootViewController:pbVC];
[self presentViewController:navVC animated:true completion:nil];
Swift Version
Here is the swift version answer for the above problem!!
let vc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "PatientBillVC") as! patientBillNavigationViewController
vc.name = "name"
let mVc = UINavigationController.init(rootViewController: vc)
self.navigationController?.present(mVc, animated: true, completion: nil)
Related
I have embedded a destination viewcontroller in a navigation controller and am now unable to pass it a variable.
My code is:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
//THis is the navigation controller
UIViewController *destVC = [storyboard instantiateViewControllerWithIdentifier:#"myNav"];
//This is the view controller embedded in the nav
IDNowVC* myVC = [storyboard instantiateViewControllerWithIdentifier:#"myVC"];
myVC.sender = #1;//for contacts
[self presentViewController:destVC animated:YES completion:nil];
After the launch of the VC, sender property is nil. It is not getting the value #1.
What am I missing?
Thanks for any suggestions.
In your code you have a comment that says:
//This is the view controller embedded in the nav
However, the view controller below that comment is not the one embedded in your navigation controller. It's a completely new controller that is created at that line and disposed of at the end of the function.
You need something more like this:
UINavigationController *destVC = (UINavigationController *)[storyboard instantiateViewControllerWithIdentifier:#"myNav"];
IDNowVC* myVC = destVC.childViewControllers[0];
myVC.sender = #1;
There might be some syntax issues with the above...
I have a view controller A in the Storyboard named "X" in it i have a button . On clicking it I am checking the success response for the API and after that I need to navigate to the view controller B in the storyboard "B" .
I am using the code:
let loginstoryboard = UIStoryboard(name: "Login", bundle: nil)
let loginController = loginstoryboard.instantiateViewController(withIdentifier: "login") as? LoginViewController
self.navigationController?.pushViewController(loginController!, animated: true)
So it is not working for me. What is the issue?
Please follow below steps.
Check your both view controllers StoryboardID (If Storyboard ID is already inserted then skip step 2).
Insert View controller StoryboardID
Please check sample image of StoryboardID
Sample Code
UIStoryboard *storyBoard = [UIStoryboard storyboardWithName:"YourStoryBoardName" bundle:nil];
UIViewController *loginViewController = [storyBoard instantiateViewControllerWithIdentifier:"LoginViewController"];
// If Login View Controller is not a Navigation Controller then you need to create Navigation Controller
UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
[self.navigationController pushViewController:viewController animated:true];
You can create reference of your story board programmatically below thenceforth instantiate viewcontroller using its storyboard id.
let loginstoryboard = UIStoryboard(name: "Login", bundle: nil)
In Storyboard
You can even reference a view controller in an external storyboard.Refer #bshirley ans for detailed explanation.
I am a little bit confused and cannot make things to work.
So, this is my code in my app Delegate.
// If the device is an iPad, we make it taller.
_tabBarController = [[AKTabBarController alloc] initWithTabBarHeight:(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) ? 70 : 50];
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
CurrentViewController * tab1VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
// dummy controllers
CurrentViewController * tab2VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
CurrentViewController * tab3VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
CurrentViewController * tab4VC = [storyboard instantiateViewControllerWithIdentifier:#"Tab1"];
[_tabBarController setViewControllers:[NSMutableArray arrayWithObjects:
tab1VC,
tab2VC,
tab3VC,
tab4VC,
nil]];
[_window setRootViewController:_tabBarController];
[_window makeKeyAndVisible];
Then, in my storyboard I have the image below :
So, I have my ViewController and I clicked Embed In > Navigation Controller
I want to have a different navigation controller for each tab.
Inside my CurrentViewController I have this when button is clicked :
- (IBAction)dummyButton:(id)sender {
NSString * storyboardName = #"Main_iPhone";
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
UserSettingsSetupViewController *userSettingsSetupController = [storyboard instantiateViewControllerWithIdentifier:#"UserSettingsSetup"];
[[self navigationController] pushViewController:userSettingsSetupController animated:YES];
}
And apparently nothing is pushed into the navigation Controller because nothing opens.
Doing some research, I saw that this is because [self navigationController] or self.navigationController returns nil.
but why is that? Should I add any code? I thought that by doing that from storyboard, it shouldn't be nil? Am I wrong?
A couple of comments. First of all, if you're using a storyboard, don't create view controllers in the app delegate; drag all the controllers you need into the storyboard. There's no need for any added code in the app delegate at all.
Your problem is caused by trying to mix storyboard controllers and code in the app delegate. You are instantiating tab1VC and making that the controller in the first tab -- that controller doesn't "know" anything about the navigation controller you added in the storyboard. If you wanted to do it in code (which I don't recommend), you would need to instantiate that navigation controller instead (it will take care of instantiating its root view controller), and add that as the first item in the viewControllers array.
My advice is do it all in the storyboard. Change the class of the tab bar controller there to AKTabBarController, and set its tab bar height in its init method or viewDidLoad.
Using a storyboard, I can have a controller have a show/push segue to a navigation controller. The new navigation controller then will have the navigationController set to the existing navigation controller and the view controllers of the new navigation controller will have the new navigation controller as their navigation controllers. If I try programmatically pushing a navigation controller it fails though. Is there any programatic way of modifying the view controllers of a navigation controller so that they can contain a navigation controller? The following won't work:
let aNavigationController = UIStoryboard(name: "OurStoryBoard", bundle: nil).instantiateViewControllerWithIdentifier("OurNavigationController") as OurNavigationController
navigationController!.pushViewControll(aNavigationController, animated: true)
NavigationControllers do not push each other. You can replace a NavigationController with another one, but that will not involve a "pushing" animation.
You can, however, set different view controllers on the same Navigation Controller and have an animation. These will replace the existing view controllers inside the Navigation Controller. Here's an example to push one View Controller and replace any existing view controllers inside the navigation controller:
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
ViewControllerOne *vc1 = [storyboard instantiateViewControllerWithIdentifier:#"ViewControllerOne"];
NSMutableArray *controllers = [NSMutableArray array];
[controllers addObject:vc1];
[yourAppNavigationController setViewControllers:controllers animated:YES];
If you want to replace the current view controller(s) with multiple view controllers:
// 1.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:#"Main" bundle:nil];
FirstViewController *vc1 = [storyboard instantiateViewControllerWithIdentifier:#"FirstViewController"];
SecondViewController *vc2 = [storyboard instantiateViewControllerWithIdentifier:#"SecondViewController"];
ThirdViewController *vc3 = [storyboard instantiateViewControllerWithIdentifier:#"ThirdViewController"];
// 2.
NSMutableArray *controllers = [NSMutableArray array];
[controllers addObject:vc1];
[controllers addObject:vc2];
[controllers addObject:vc3];
// 3.
[yourAppNavigationController setViewControllers:controllers animated:YES];
Again, this involves the "pushing" animation that you are probably looking for, and it replaces the existing view controllers so there won't be an undesired back button (in the first code example w/ one VC).
The code about present another independent storyboard as follows:
- (IBAction)action:(id)sender {
UIStoryboard *secondStoryboard = [UIStoryboard storyboardWithName:#"SecondStoryboard" bundle:nil];
UIViewController *firstVC = [secondStoryboard instantiateViewControllerWithIdentifier:#"22"];
[self.navigationController presentViewController:firstVC animated:YES completion:nil];
}
If my storyboard like follow contain a UITabBarController the Pesentbutton with function -(IBAction)action:(id)sender; can't present another independent storyboard.
If my storyboard like follow contain a UINavigationController the Pesentbutton with function -(IBAction)action:(id)sender; presents another independent storyboard as expected.
Who can tell me the reason?More thanks!
Because in the first case self.navigationController is nil... Since no navigation controller is present. Try presenting from self or self.tabBarController