将数据从View Controller传递到Swift中的Child Controller [英] Pass data from View Controller to Child Controller in Swift

查看:137
本文介绍了将数据从View Controller传递到Swift中的Child Controller的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个视图控制器(VC1,父视图控制器和子视图控制器)。加载ParentVC时,如何将数据从VC1传递到子视图控制器?通常我可以在第一视图控制器中使用它

I have three View Controllers (VC1, Parent View Controller and Child View Controller). How do I pass data from the VC1 to the child View controller when the ParentVC is loaded? Normally I would be able to use this in the First View Controller

var text = "Hello"
var sVC = SecondViewController()
sVC.string = text

它会传递 Hello 到第二个视图控制器中的变量 string 。然后做同样的事情将数据从第二视图控制器传递到第三个。但不幸的是,这种逻辑不起作用。我试图将数据从VC1传递给ParentVC然后传递给ChildVC。我也尝试将数据从VC1直接传递给ChildVC,但这似乎也不起作用。

and it would pass Hello to the variable string in the second view controller. and then do the same thing to pass data from the Second View Controller to the third one. But unfortunately, this logic is not working. I've tried to pass the the data from VC1 to the ParentVC then to the ChildVC. I've also tried to pass the data from VC1 directly to the ChildVC but that does not seem to work either.

这是我所拥有的

import UIKit
class ViewController1: UIViewController {

var a = "Test"

override func viewDidLoad() {
   var pVC = ParentViewController()
   pVC.a = a
  }
}

我能够将数据传递给父视图控制器,它打印测试,但它不适用于子视图控制器

I'm able to pass data to the Parent View Controller and it prints Test but it does not for the Child View Controller

import UIKit

class ParentViewController: UIViewController {

var pageMenu: CAPSPageMenu?
var a = ""

 func test() {

  var cvC = ChildViewController(frame: self.view.frame)
   cvC.a = a
}

override func viewDidLoad() {
   println(a)

}

override func viewWillAppear(animated: Bool) {

}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)

    // MARK: - Scroll menu setup

    // Initialize view controllers to display and place in array
    var controllerArray : [UIViewController] = []

    var controller1 : ChildViewController = ChildViewController(nibName: nil, bundle: nil)
    controller1.title = "View Controller"
    controllerArray.append(controller1)

    // Initialize scroll menu
    pageMenu = CAPSPageMenu(viewControllers: controllerArray, frame: CGRectMake(0.0, 65, self.view.frame.width, self.view.frame.height), pageMenuOptions: parameters)

    self.view.addSubview(pageMenu!.view)

    }
}

在孩子身上View Controller我有:

And in the Child View Controller I have:

import UIKit

class ChildViewController: UIViewController {

var a = ""

convenience init(frame:CGRect){
    self.init(nibName: nil, bundle: nil)
    self.view.frame = frame

}

override func viewDidLoad() {
   var pVC = ParentViewController()
   pVC.test()
   println(a)
}  


推荐答案

加载<$ c时来自 ViewController 的$ c> ParentViewController ,没有视图加载到内存中。它只是 ParentViewController 类的一个实例。

When loading the ParentViewController from your ViewController, there are no views loaded into memory. It's just an instance of your ParentViewController class.

override func viewDidLoad() {
   var pVC = ParentViewController()
   pVC.a = a
}

我猜你在代码中的其他地方加载或显示这个控制器。如果是这样,你成功打印正在设置的变量是有意义的。

And i guess you are loading or presenting this controller somewhere else in your code. If so, it makes sense that you're successfully printing the variable being set.


println(a) - >测试

println(a) -> "Test"

但是,当你实例化你的 ChildViewController 时,你正在为它提供一个框架 convenience init ,您实际上是在启动视图层次结构,而后者又会触发 viewDidLoad 事件。这是在从 ParentViewController - >设置 a 变量之前执行的。 viewDidLoad中。打印 a 变量的结果实际上是一个空字符串。

However, when you are instantiating your ChildViewController, you are providing a frame for its convenience init and you are actually initiating the view hierarchy, which in turn fires the viewDidLoad event. This is executed before the a variable is being set from ParentViewController -> ViewDidLoad. And your result from printing the a variable is actually an empty string.

一个选项是将逻辑放在你明确调用它们自己的函数。 (可选)将数据作为参数,或者在调用函数之前设置变量。

One option is to put your logic within their own functions that you explicitly call. Optionally with your data as parameters or you set the variables before calling the function(s).

您应该查看其中的一些内容,以便更好地了解其工作原理和你可以遵循的路径来完成你想要的。

You should check out some of these to get a better understanding of how things work and paths you can follow to accomplish what you want.

在Swift中实例化并呈现一个viewController

UIViewController类引用

如何将数据从父视图控制器传递到子视图控制器?

希望这有帮助!

这篇关于将数据从View Controller传递到Swift中的Child Controller的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆