How to pass data from child to parent view controller? in swift

前端 未结 5 707
攒了一身酷
攒了一身酷 2020-12-16 02:05

I created an activity where when one of the text fields clicked it will pop up a child(alert dialog) with list of product but when i click one item on the list I can\'t disp

5条回答
  •  有刺的猬
    2020-12-16 02:18

    You can use protocols/delegate

    Here is a very very straightforward explanation, no bs: https://www.youtube.com/watch?v=guSYMPaXLaw

    Or in your situation you can also use NSNotificationCenter

    You can do something like this:

    The "sender" view controller would do

    let nc = NSNotificationCenter.defaultCenter()
    nc.postNotificationName("printValue", object: nil, userInfo: ["value" : "Pass Me this string"])
    

    The receiver view controller then can listen to the notification.

    let nc = NSNotificationCenter.defaultCenter()
    nc.addObserver(self, selector: #selector(printValue), name: "printValue", object: nil)
    
    func printValue(notification:NSNotification) {
        let userInfo:Dictionary = notification.userInfo as! Dictionary
        let item = userInfo["value"]! as String
    
        print(item,self)
    }
    

提交回复
热议问题