简体   繁体   English

迅捷的代表和协议

[英]Swift Delegates and Protocol

I am learning swift protocol and delegate pattern.But,I really need help with the following problem which return me empty results 我正在学习快速的协议和委托模式。但是,我确实需要以下问题的帮助,这些问题使我返回空结果

At ViewController 1 : 在ViewController 1处:

protocol GetAllDataProtocol{
     func didReadInfo(info : [[String:String]])
}
class ViewController1 : {
     var delegate : GetAllDataProtocol?
     var info_data : [[String:String]] = []

     override func viewDidLoad() {

         readData()
     }

     func readData(){
         info_data.append(["ID" :"234FD","ADDRESS":"Maimi","TYPE":"A"])
         delegate?.didReadInfo(info_data)
     }
}

Then at ViewController 2 : 然后在ViewController 2:

class SubmissionViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate,GetAllDataProtocol{
      var info_data = [[String:String]]()

      @IBAction func submitTapped(sender: AnyObject) {
           print("After : \(info_data)")
      }

      func didReadInfo(info: [[String : String]]) {
           dispatch_async(dispatch_get_main_queue(),{
               self.info_data = info
               print("Before : \(self.info_data)")
           })
      }
}

When it run 运行时

After : []

Why it didnt run "before?" 为什么它没有“之前”运行? and why i can't get the data. 以及为什么我无法获取数据。

SubmissionViewController should have referenece on ViewController1. SubmissionViewController应该在ViewController1上具有参考值。 In viewDidLoad() method assign viewController.delegate to self. 在viewDidLoad()方法中,将viewController.delegate分配给self。

The code: 编码:

class SubmissionViewController: UIViewController,UIPickerViewDataSource,UIPickerViewDelegate,GetAllDataProtocol{
    var info_data = [[String:String]]()
    var viewController1:ViewController1?

    override func viewDidLoad() {
        //You should here load viewController1 or connect it by outlet in above
        let storyboard = UIStoryboard(name: "MyStoryboardName", bundle: nil)
        viewController1 = storyboard.instantiateViewControllerWithIdentifier("ViewController1") as? ViewController1
        self.viewController1!.delegate = self
    }
    @IBAction func submitTapped(sender: AnyObject) {
        print("After : \(info_data)")
    }

    func didReadInfo(info : [[String:String]]) {
        dispatch_async(dispatch_get_main_queue(),{
            self.info_data = info
            print("Before : \(self.info_data)")
        })
    }
}

You aren't setting the reference of the delegate as Ahmed Lotfy explained. 您没有像Ahmed Lotfy所解释的那样设置代表的引用。

Maybe you are missing the point that a protocol only is a kind of a template or commitment. 也许您错过了仅协议只是模板或承诺的一种观点。 So if you define a protocol with your function 因此,如果您使用函数定义协议

 func didReadInfo(info : [[String:String]])

you are saying "Hey, every class that conforms to the protocol XYZ should have implemented this method." 您说的是:“嘿,每个符合XYZ协议的类都应该实现此方法。” And by declaring a delegate variable on your "ViewController1" class that should be conform to protocol "GetAllDataProtocol", you just ensure that you can call the function "didReadInfo" on that object. 通过在“ ViewController1”类上声明一个应符合协议“ GetAllDataProtocol”的委托变量,您只需确保可以在该对象上调用函数“ didReadInfo”。 But this variable is not set anywhere, thats why your call 但是这个变量没有设置在任何地方,这就是为什么你的电话

delegate?.didReadInfo(info_data)

has no effect. 没有效果。 The optional chaining of "delegate?." “委托?”的可选链接。 does not succeeds and the function "didReadInfo" is never called. 不会成功,并且永远不会调用函数“ didReadInfo”。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM