How to pass data between UIViewControllers with protocols/delegates
Asked Answered
B

5

10

In the code below I have a ViewController("SenderViewController"), which passes a message to the main ViewController when a button is tapped. What I don't fully understand is how does messageData() method in the main ViewController know when to listen for the message.

Can someone please explain me what is triggering the messageData() method in the main ViewController?

SenderViewController:

import UIKit  
protocol SenderViewControllerDelegate {  
    func messageData(data: AnyObject)  
}  
class SenderViewController: UIViewController {  
    @IBOutlet weak var inputMessage: UITextField!  
     var delegate: SenderViewControllerDelegate?  

    @IBAction func sendData(sender: AnyObject) {  
        /  
        if inputMessage.text != ""{  
            self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)  
            self.delegate?.messageData(inputMessage.text!)  
        }  
    }  
}  

Main ViewController:

import UIKit  
class ViewController: UIViewController, SenderViewControllerDelegate{  
    @IBOutlet weak var showData: UILabel!  

    override func viewDidLoad() {  
        super.viewDidLoad()  
    }  

    @IBAction func goToView(sender: AnyObject) {  
        let pvc = storyboard?.instantiateViewControllerWithIdentifier("senderViewController") as! SenderViewController  
        pvc.delegate = self  
        self.presentViewController(pvc, animated:true, completion:nil)  
    }  

   // What triggers this method, how it know when to listen? 
    func messageData(data: AnyObject) {  
        self.showData.text = "\(data)"  
    }  
} 

Thanks a lot!

Bluebottle answered 2/8, 2016 at 13:10 Comment(3)
This call self.delegate?.messageData from func sendData in SenderViewController is executing it.Jennijennica
Follow this tutorial, you will get your answer - tutorialspoint.com/ios/ios_delegates.htmHeida
Passing data using delegate swift 4.0 : iosdevcenters.blogspot.com/2017/11/…Electronic
F
6

Objects don't exactly listen for method calls. They sit there, waiting to invoked.

The line

self.delegate?.messageData(inputMessage.text!)

From your SenderViewController is a function call. (The term method and function are pretty much interchangeable, although the method is usually used for the functions of objects.) It invokes the function messageData in ViewController.

Fountain answered 2/8, 2016 at 13:16 Comment(0)
A
3

While Presenting SenderViewController from MainViewController you are setting the delegate as self. So whenever you call the delegate method in SenderViewController

self.delegate?.messageData(inputMessage.text!)

following method of MainViewController will act as a callback

func messageData(data: AnyObject) {  
        self.showData.text = "\(data)"  
    } 
Atalee answered 2/8, 2016 at 13:19 Comment(1)
Thanks a lot for your input.Bluebottle
D
3

In SenderViewController:

When you tap button you invoke sendData method. In this method you ask delegate to invoke its messageData method. Delegate property declared as SenderViewControllerDelegate type, so you can do that (see this protocol defenition).

In ViewController (first view controller):

Before you open second view controller, in method goToView you seting up property delegate of SenderViewController to 'myself', to exact instance of ViewController, since you declared that it confirm protocol SenderViewControllerDelegate by implementing method messageData. So, ViewController is now saved as delegate property in SenderViewController, and can be used to invoke messageData!

Dawna answered 2/8, 2016 at 13:19 Comment(0)
R
2
self.delegate?.messageData(inputMessage.text!)
Rezzani answered 2/8, 2016 at 13:46 Comment(0)
G
0
@IBAction func sendData(sender: AnyObject) {  

    if inputMessage.text != ""{  
        self.delegate?.messageData(inputMessage.text!)  
        self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)  

    }else{
       //handle here
}

Note: If you need to pass multiple data to mainViewController then use dictionary to pass them. i.e.

SenderViewController:

import UIKit  
protocol SenderViewControllerDelegate {  
    func messageData(data: [String : Any])  
}  
class SenderViewController: UIViewController {  
    @IBOutlet weak var inputMessage: UITextField!  
    var delegate: SenderViewControllerDelegate?  

    @IBAction func sendData(sender: AnyObject) {  

        let myDict = [ "name": "Name", "age": 21, "email": "[email protected]"] as! [String : Any]

        self.delegate?.messageData(myDict) 
        self.presentingViewController!.dismissViewControllerAnimated(true, completion: nil)  


  }  
}  

Main ViewController

import UIKit  
class ViewController: UIViewController, SenderViewControllerDelegate{  
    @IBOutlet weak var showData: UILabel!  

    override func viewDidLoad() {  
        super.viewDidLoad()  
    }  

    @IBAction func goToView(sender: AnyObject) {  
        let pvc = storyboard?.instantiateViewControllerWithIdentifier("senderViewController") as! SenderViewController  
        pvc.delegate = self  
        self.presentViewController(pvc, animated:true, completion:nil)  
    }  

   // What triggers this method, how it know when to listen? 
    func messageData(data: [String : Any]) {  
        print(data["name"])  
        print(data["age"])  
        print(data["email"])  

    }  
} 
Geopolitics answered 18/12, 2018 at 8:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.