【问题标题】:How to pass value to a viewController embedded in NavigationController如何将值传递给嵌入在 NavigationController 中的 viewController
【发布时间】:2017-02-22 23:29:48
【问题描述】:

有类似的问题,但它们要么很快,要么没有解决我的问题。
我有一个视图控制器,当单元格确实按下选择按钮时,它会显示导航视图控制器:

patientBillNavigationViewController *viewController = [self.storyboard instantiateViewControllerWithIdentifier:@"PatientBillVC"];
//soem value assignment
[self presentViewController:viewController animated:YES completion:nil];

您可以说计费打开了一个全新的视图,独立于主应用流程来处理计费流程。
此导航视图控制器自动加载的视图是帐单视图,现在如果我想将此视图控制器中的值传递给嵌入导航视图中的另一个视图控制器,我不能这样做。如何传递一个值?

UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle: nil];
            PatientBillViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"PBVC"];

            vc.superBillToAddEdit = sb;
//then display the view embedded in navigation view

但这不起作用。

【问题讨论】:

  • 你为什么不使用delegate protocol呢?在呈现之前将视图控制器设置为 PatientBillViewController 的代表。

标签: ios objective-c uinavigationcontroller storyboard


【解决方案1】:

我知道的一种方法是子类UINavigationViewController 并在其viewDidLoad 中,您可以这样做:

 YourEmbeddedVc *svc =self.viewControllers[0]; //get the controller reference
 svc.name= @"Hello";

我假设您的导航堆栈中只有一个控制器,如果没有,您需要找出所需 VC 的正确匹配。

【讨论】:

    【解决方案2】:

    我可能没有完全理解这个问题,如果我错了,请纠正我。

    听起来您想从视图控制器(我们称之为firstViewController)启动导航控制器,并将firstViewController 中的值传递给最终将加载到导航控制器中的视图控制器。

    如果是这样,为什么不创建第二个视图控制器 (billingStep1ViewController),然后将值作为属性分配给它,然后将 billingStep1ViewController 传递给导航控制器的 initWithRootViewController: 方法?

    类似这样的东西(未经测试的代码,顺便说一句):

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

    这样您就可以让两个视图控制器直接相互通信,而完全不用担心导航控制器。

    更新:这里有一些测试代码可以用来说明我的想法:

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

    有些类名改变了,但思路是一样的。

    【讨论】:

      【解决方案3】:

      与其从故事板标识符创建导航控制器,不如以编程方式创建它,使用您创建的PatientBillViewController 作为其根视图控制器。像这样:

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

      【讨论】:

        【解决方案4】:

        Swift 版本

        这是上述问题的快速版本答案!!

           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)
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2020-11-14
          • 2020-05-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多