Chaudhry Talha
Chaudhry Talha

Reputation: 7878

How to pass value to a viewController embedded in NavigationController

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.

Upvotes: 0

Views: 287

Answers (4)

Vishnu Hari
Vishnu Hari

Reputation: 113

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)

Upvotes: 0

JAB
JAB

Reputation: 3235

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];

Upvotes: 0

grego
grego

Reputation: 512

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.

Upvotes: 1

Teja Nandamuri
Teja Nandamuri

Reputation: 11201

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.

Upvotes: 1

Related Questions