How to Pass information Back in iOS when reversing a Segue using Swift?

后端 未结 3 1604
滥情空心
滥情空心 2020-12-09 06:14

I have two view controllers, One and Two. I go from VC One to VC Two. On VC Two, I select some data that I store in an array. When I press the \"Back\" button on the navigat

3条回答
  •  臣服心动
    2020-12-09 06:24

    You can also use the Notification Design Pattern (Post & Observe), which is mainly used to pass on the same object/information from one VC to multiple View Controllers.

    For your scenario : In VC2.swift :

    @IBAction func BackBtn(sender: UIButton) {  
      NSNotificationCenter.defaultCenter().postNotificationName("ThisIsTheMessage", object: nil, userInfo:["ObjectBeingSent":yourObject])
    
    }
    

    And in VC1.swift :

    override func viewDidLoad() {
            super.viewDidLoad()
            NSNotificationCenter.defaultCenter().addObserver(self, selector: Selector("yourFunction:"), name: "ThisIsTheMessage", object: nil)
        }
    
    
    
    func yourFunction(theNotification : NSNotification) {
    
               if let extractInfo = theNotification.userInfo {
         //code to use the object sent from VC2, by extracting the object details
        }
         }
    

提交回复
热议问题