0

I am creating a library in IOS/swift that:

takes a user to a scene --> performs a task --> return to the initial scene that called the first while passing a payload back to the user

I have figured out how to take users back to the previous scene that called it, but my issue is how to send a payload back with it using thee code snippet below:

func switchToPreviousPage(){
    self.dismiss(animated: true, completion: nil)
}

How do I achieve this?

Oto-Obong Eshiett
  • 527
  • 1
  • 8
  • 17
  • What kind of data are you trying to send back? Protocols are good for these things. Look if this helps: https://stackoverflow.com/a/38721515/4189589 – maniponken May 07 '20 at 11:06
  • @maniponken an object with a string property – Oto-Obong Eshiett May 07 '20 at 12:30
  • Have a look at this post: https://stackoverflow.com/a/37203430/2583679 – Tom May 07 '20 at 12:54
  • This question is a very common one, and there are [lots](https://stackoverflow.com/q/5210535/643383) [of](https://stackoverflow.com/q/24222640/643383) [duplicates](https://stackoverflow.com/q/30431426/643383). – Caleb May 07 '20 at 13:20

1 Answers1

1

In your scenario you can use either :

  1. Delegation Pattern
  2. Notification/Observer

Lets discuss each one :

1. Delegation :

If you have idea about Protocol in Swift you can do it easily. first create a protocol with the required function you want to implement :

protocol FirstControllerDelegate: AnyObject {
    func sendData(data: String)
}

Suppose your firstPage is FirstViewController, it has a UILabel and we have to assign a String to it from our secondPage means SecondViewController. the Structure of your FirstViewController may be like this :

class FirstViewController: UIViewController {
    @IBOutlet weak var textLabel: UILabel!
    @IBAction func gotoSecondPage() {
        let secondVC = storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
    }

}

Now your FirstViewController has to confirm to this protocol and it will implement the sendData(data: ) method :

extension FirstViewController: FirstControllerDelegate {
    func sendData(data: String) {
        textLabel.text = data
    }
}

Now as a feature of Protocol in iOS, Protocols can work as a Type(like Int, String). So just create a variable of type FirstControllerDelegate in your SecondViewController !

class SecondViewController: UIViewController {
    weak var delegate: FirstControllerDelegate!
    @IBAction func switchToPreviousPage() {
        delegate.sendData(data: "Hello")
        self.dismiss(animated: true, completion: nil)
    }
}

You can now call the sendData(data:) function with the variable you created above !

At last you have to do oneThing just assign the delegate :

secondVC.delegate = self

It should be inside the gotoSecondPage() method !

2. Notification/Observer

With this, our basic idea is to send a Notification inside our app, and it can be observed by any where inside !

So our SecondViewController will send a Notification embedded with required data that we want to pass, and FirstViewController will receive the Notification and it will extract the data from the Notification !!

Each Notification has a specific name, which will differentiate it from other Notifications. we have to create the Name :

Notification.Name(rawValue: "com.app.notificationObserver")

Now the FirstViewController will be Observe to this specific notification :

override func viewDidLoad() {
        super.viewDidLoad()
        NotificationCenter.default.addObserver(self, selector: #selector(self.changeLabelText(notifcation:)), name: Notification.Name("com.app.notificationObserver"), object: nil)
    }

We have to define changeLabelText(notification:) method :

private func changeLabelTExt(notification: NSNotification) {
    if let dataDict = notification.userInfo as NSDictionary? {
        if let message = dataDict["data"] as? String {
            self.textLabel.text = message
        }
    }
}

Finally, SecondViewController will trigger the Notification :

@IBAction func switchToPreviousPage() {
        NotificationCenter.default.post(name: Notification.Name(rawValue: "com.app.notificationObserver"), object: ["data": "hello"])
        self.dismiss(animated: true, completion: nil)
    }

Thats All .....

Debashish Das
  • 859
  • 4
  • 14