简体   繁体   English

如何将值传递给嵌入在 NavigationController 中的 viewController

[英]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.但这是行不通的。

One way I know is to Subclass UINavigationViewController and in its viewDidLoad , you can do:我知道的一种方法是继承UINavigationViewController并在其viewDidLoad ,您可以执行以下操作:

 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.我假设您的导航堆栈中只有一个控制器,否则您需要找出所需 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.听起来您想从视图控制器(我们称之为firstViewController )启动一个导航控制器,并将一个值从firstViewController传递给最终将加载到导航控制器中的视图控制器。

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?如果是这种情况,为什么不创建第二个视图控制器( billingStep1ViewController ),然后将值作为属性分配给它,然后将 billingStep1ViewController 传递给导航控制器的initWithRootViewController:方法?

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.不是从故事板标识符创建导航控制器,而是以编程方式创建它,使用您创建的PatientBillViewController作为其根视图控制器。 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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用StoryBoard在NavigationController和ViewController之间传递值? - How can I pass value between NavigationController and ViewController with StoryBoard? 如何从ViewController选择(推送)到NavigationController中嵌入的另一个ViewController? - How to segue (push) from ViewController to another ViewController embedded in NavigationController? 如何使用 navigationController 将 viewmodel 传递给 viewcontroller? - How to pass viewmodel to a viewcontroller using navigationController? 如何从嵌入在TabController中的NavigationController内的ViewController访问tabBarItem? - How to access tabBarItem from ViewController inside of NavigationController embedded in TabController? 如何将框架传递给嵌入式ViewController? - How to pass a frame to embedded ViewController? 如何将值从嵌入式collectionView中的选定单元格传递到另一个viewController? - How to pass value from a selected cell in embedded collectionView to another viewController? 如何使用 NavigationController 将数据从 UIPickerView 传递到上一个 ViewController - How to Pass data from UIPickerView to previous ViewController with NavigationController 从TableViewCell跳转到NavigationController中嵌入的ViewController - Jump from a TableViewCell to a ViewController embedded in a NavigationController 如何打开NavigationController的根并传递值? - How to open NavigationController's root and pass a value? 如何将变量传递给嵌入在导航 Controller 中的 ViewController? - How to pass variable to a ViewController that is embedded in a Navigation Controller?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM