How to pass values from a Pop Up View Controller to the Previous View Controller?

前端 未结 3 2081
星月不相逢
星月不相逢 2021-01-26 06:04

So In my 1stViewController I have this code:

@IBAction func colorDropdown(_ sender: Any) {
    self.popUpColorPicker()
}

func popUpColorPicker() {
    let popOv         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-26 06:16

    Suppose A & B are two controllers and you first navigated from A to B with some data. And now you want to POP from B to A with some data.

    Unwind Segues is the best and recommended way to do this. Here are the steps.

    1. Open A.m
    2. define following method

      @IBAction func unwindSegueFromBtoA(segue: UIStoryNoardSegue) {

      }

    3. open storyboard

    4. Select B ViewController and click on ViewController outlet. press control key and drag to 'Exit' outlet and leave mouse here. In below image, selected icon is ViewController outlet and the last one with Exit sign is Exit Outlet.

    5. You will see 'unwindSegueFromBtoA' method in a popup . Select this method .

    6. Now you will see a segue in your view controler hierarchy in left side. You will see your created segue near StoryBoard Entry Piont in following Image.

    1. Select this and set an identifier to it. (suggest to set the same name as method - unwindSegueFromBtoA)

    2. Open B.m . Now, wherever you want to pop to A. use

      self.performSegueWithIdentifier("unwindSegueFromBtoA", sender: dataToSend)

    3. Now when you will pop to 'A', 'unwindSegueFromBtoA' method will be called. In unwindSegueFromBtoA of 'A' you can access any object of 'B'.

    4. That's it..!

提交回复
热议问题