在childViewControllers Swift之间传递数据

时间:2018-05-23 11:31:47

标签: ios swift

我在container内放了一个MainViewController。然后在FirstViewController

中添加了container
let containerView = UIView()
view.addSubview(containerView)

let controller = FirstViewController()
addChildViewController(controller)
containerView.addSubview(controller.view)

controller.didMove(toParentViewController: self)

UITextField内有UIButtonFirstViewControllerSecondViewControllervar passedText = ""中有一个标签可以更新UILabel文本。我只想在按下UIButton时根据UITextField中的FirstViewController文本设置UILabel文本,它也应该导致SecondViewController。这是来自FirstViewController的UIButton操作方法。我没有使用Storyboard

@IBAction func btnPressed(_ sender: Any) {
   let secondVC = SecondViewController()
   addChildViewController(secondVC)
   view.addSubview(secondVC.view)

   secondVC.view.frame = view.bounds
   secondVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

   secondVC.didMove(toParentViewController: self)
   secondVC.passedText = inputTextField.text!
}

这是FirstViewController

class FirstViewController: UIViewController {

  @IBOutlet weak var inputTextField: UITextField!

  let secondVC = SecondViewController()

  override func viewDidLoad() {
    super.viewDidLoad()
  }

  @IBAction func btnPressed(_ sender: Any) {
    addChildViewController(secondVC)
    view.addSubview(secondVC.view)

    secondVC.view.frame = view.bounds
    secondVC.view.autoresizingMask = [.flexibleWidth, .flexibleHeight]

    secondVC.didMove(toParentViewController: self)
    secondVC.passedText = inputTextField.text!
  }        
}

这是SecondViewController

class SecondViewController: UIViewController {

  var passedText = ""

  @IBOutlet weak var label: UILabel!

  override func viewDidLoad() {
    super.viewDidLoad()

    label.text = passedText
  }
}

3 个答案:

答案 0 :(得分:1)

  

封闭

class ParentViewController: UIViewController {

    var embeddedViewController: UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        let firstViewController = FirstViewController()
        embedViewController(firstViewController)
        firstViewController.passDataClosure = { [weak self] text in
            let secondViewController = SecondViewController()
            self?.embedViewController(secondViewController)
            secondViewController.passedText = text
        }
    }

    func embedViewController(_ viewController: UIViewController) {
        removeEmbedded()
        viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        addChildViewController(viewController)
        view.addSubview(viewController.view)
        viewController.didMove(toParentViewController: self)
        embeddedViewController = viewController
    }

    func removeEmbedded() {
        embeddedViewController?.view.removeFromSuperview()
        embeddedViewController?.didMove(toParentViewController: nil)
        embeddedViewController?.removeFromParentViewController()
    }
}

-

class FirstViewController: UIViewController {

    var passDataClosure: ((String?) -> Void)?

    @IBOutlet weak var inputTextField: UITextField!

    @IBAction func btnPressed(_ sender: Any) {
        passDataClosure?(inputTextField.text)
    }
}

-

class SecondViewController: UIViewController {

    var passedText: String? {
        didSet {
            label.text = passedText
        }
    }

    @IBOutlet weak var label: UILabel!
}
  

代表

class ParentViewController: UIViewController, FirstViewControllerDelegate {

    var embeddedViewController: UIViewController?

    override func viewDidLoad() {
        super.viewDidLoad()

        let firstViewController = FirstViewController()
        embedViewController(firstViewController)
        firstViewController.delegate = self
    }

    func embedViewController(_ viewController: UIViewController) {
        removeEmbedded()
        viewController.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
        addChildViewController(viewController)
        view.addSubview(viewController.view)
        viewController.didMove(toParentViewController: self)
        embeddedViewController = viewController
    }

    func removeEmbedded() {
        embeddedViewController?.view.removeFromSuperview()
        embeddedViewController?.didMove(toParentViewController: nil)
        embeddedViewController?.removeFromParentViewController()
    }

    // MARK: FirstViewControllerDelegate
    func firstViewController(_ firstViewController: FirstViewController, pass text: String?) {
        let secondViewController = SecondViewController()
        embedViewController(secondViewController)
        secondViewController.passedText = text
    }
}

-

class SecondViewController: UIViewController {

    var passedText: String? {
        didSet {
            label.text = passedText
        }
    }

    @IBOutlet weak var label: UILabel!
}

-

protocol FirstViewControllerDelegate: class {
    func firstViewController(_ firstViewController: FirstViewController, pass text: String?)
}

class FirstViewController: UIViewController {

    weak var delegate: FirstViewControllerDelegate?

    @IBOutlet weak var inputTextField: UITextField!

    @IBAction func btnPressed(_ sender: Any) {
        delegate?.firstViewController(self, pass: inputTextField.text)
    }
}

答案 1 :(得分:0)

据我所知,这应该有效:

secondVC.label.text = inputTextField.text

您目前有secondVC.label = inputTextField.text,甚至不应该编译。

答案 2 :(得分:-1)

您可以为场景创建单例类。这样您就可以从firstViewController或SecondViewController中随处访问所有重要数据。

class YourDataClass : NSObject{

var textFieldData = String()
var lblSeconddata = String()
 struct Static
    {
        public static var instance: YourDataClass?
    }

    class var singleton: YourDataClass
    {
        if Static.instance == nil
        {
            Static.instance = YourDataClass()
        }

        return Static.instance!
    }
}

现在控制器中的某个地方

YourDataClass.Singleton.textFieldData = txtFld.Text
YourDataClass.Singleton. lblSeconddata = lblText.Text

以极快的方式你可以随时随地获取