<i id='qNB0R'><tr id='qNB0R'><dt id='qNB0R'><q id='qNB0R'><span id='qNB0R'><b id='qNB0R'><form id='qNB0R'><ins id='qNB0R'></ins><ul id='qNB0R'></ul><sub id='qNB0R'></sub></form><legend id='qNB0R'></legend><bdo id='qNB0R'><pre id='qNB0R'><center id='qNB0R'></center></pre></bdo></b><th id='qNB0R'></th></span></q></dt></tr></i><div id='qNB0R'><tfoot id='qNB0R'></tfoot><dl id='qNB0R'><fieldset id='qNB0R'></fieldset></dl></div>
  • <legend id='qNB0R'><style id='qNB0R'><dir id='qNB0R'><q id='qNB0R'></q></dir></style></legend>
    • <bdo id='qNB0R'></bdo><ul id='qNB0R'></ul>

    <small id='qNB0R'></small><noframes id='qNB0R'>

    <tfoot id='qNB0R'></tfoot>

        在两个 ViewController(委托)之间传递数据 - Swift

        时间:2023-05-16
          <bdo id='Tst8Q'></bdo><ul id='Tst8Q'></ul>

        • <i id='Tst8Q'><tr id='Tst8Q'><dt id='Tst8Q'><q id='Tst8Q'><span id='Tst8Q'><b id='Tst8Q'><form id='Tst8Q'><ins id='Tst8Q'></ins><ul id='Tst8Q'></ul><sub id='Tst8Q'></sub></form><legend id='Tst8Q'></legend><bdo id='Tst8Q'><pre id='Tst8Q'><center id='Tst8Q'></center></pre></bdo></b><th id='Tst8Q'></th></span></q></dt></tr></i><div id='Tst8Q'><tfoot id='Tst8Q'></tfoot><dl id='Tst8Q'><fieldset id='Tst8Q'></fieldset></dl></div>

          <small id='Tst8Q'></small><noframes id='Tst8Q'>

          <legend id='Tst8Q'><style id='Tst8Q'><dir id='Tst8Q'><q id='Tst8Q'></q></dir></style></legend>

              1. <tfoot id='Tst8Q'></tfoot>
                  <tbody id='Tst8Q'></tbody>

                • 本文介绍了在两个 ViewController(委托)之间传递数据 - Swift的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  限时送ChatGPT账号..

                  我有两个 ViewController.

                  1. FirstVC - 我有 labelbutton 与 segue "modal"

                  1. FirstVC - I have label and button with segue "modal"

                  SecondVC - 我有 PickerViewbutton(返回 FirstVC):

                  SecondVC - I have PickerView and button (back to FirstVC):

                  @IBAction func bntback(sender: AnyObject) {
                            self.dissmissViewControllerAnimatied(true, completion: nil)
                      }
                  

                  我在 SecondViewController 中创建了委托:

                  And I created delegate in SecondViewController as:

                  protocol SendDataDelegate {
                     func sendData(text:String)
                  }
                  

                  下一步:

                  class SecondVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
                        var delegate: SendDataDelegate!
                        var firstvc = FirstVC()
                        var arr = ["First", "Second", "Third"]
                        @IBOutlet var pickview: UIPickerView!
                        override func viewDidLoad() {
                           super.viewDidLoad()
                           pickview.dataSource = self
                           pickview.selegate = self
                       }
                  

                  我的 PickerView 函数,在这个函数中我使用我的委托作为:

                  My function of PickerView and in this function I use my delegate as:

                  func pickerView (pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
                    var text = arr[row]
                    dispatch_async(dispatch_get_main_quene(), {
                    self.delegate.sendData(text)//there is an error: "fatal error: unexpectedly found nil while unwrapping an Optional value"
                    )}
                  }
                  

                  第一VC:

                  class FirstVC: UIViewController, SendDataDelegate {
                        var data = SecondVC()
                        //....
                        override func viewDidLoad() {
                           super.viewDidLoad()
                           self.data.delegate = self
                       }
                       func sendData (text:String) {
                          mylable.text = text
                          //or 
                          //var txt = text
                         //mylable.text = txt
                       }
                   }
                  

                  请帮我解决这个问题.

                  推荐答案

                  1) 你需要将delegate设置成prepareForSegue:

                  1) You need to set delegate into prepareForSegue:

                  override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
                      if let viewController = segue.destinationViewController as? SecondVC {
                          viewController.delegate = self
                      }
                  }
                  

                  更新:

                  2) 将 delegate 设置为可选

                  2) Set delegate as Optional

                  class SecondVC: UIViewController, UIPickerViewDataSource, UIPickerViewDelegate {
                      var delegate: SendDataDelegate?
                      ...
                  
                      func pickerView (pickerView: UIPickerView, didSelectRow row: Int, inComponent component: Int) {
                          var text = arr[row]
                          dispatch_async(dispatch_get_main_quene(), {
                              self.delegate?.sendData(text)
                          )}
                      }
                  

                  这篇关于在两个 ViewController(委托)之间传递数据 - Swift的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何快速居中弹出视图 下一篇:一次关闭多个模式视图控制器?

                  相关文章

                • <legend id='O2xfo'><style id='O2xfo'><dir id='O2xfo'><q id='O2xfo'></q></dir></style></legend>
                  <i id='O2xfo'><tr id='O2xfo'><dt id='O2xfo'><q id='O2xfo'><span id='O2xfo'><b id='O2xfo'><form id='O2xfo'><ins id='O2xfo'></ins><ul id='O2xfo'></ul><sub id='O2xfo'></sub></form><legend id='O2xfo'></legend><bdo id='O2xfo'><pre id='O2xfo'><center id='O2xfo'></center></pre></bdo></b><th id='O2xfo'></th></span></q></dt></tr></i><div id='O2xfo'><tfoot id='O2xfo'></tfoot><dl id='O2xfo'><fieldset id='O2xfo'></fieldset></dl></div>

                    <small id='O2xfo'></small><noframes id='O2xfo'>

                        <bdo id='O2xfo'></bdo><ul id='O2xfo'></ul>
                      <tfoot id='O2xfo'></tfoot>