Pass data from one view to another with protocol's

1

Having the following code:

ViewController2:

import UIKit

protocol TransferirTexto {

    func transferir(texto : String)

}

class SecondViewController: UIViewController {

    @IBOutlet weak var textField: UITextField!

    var delegate : TransferirTexto?


    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }




    @IBAction func transferirDato(sender: AnyObject) {

        delegate?.transferir(textField.text!)
        self.navigationController?.popViewControllerAnimated(true)
    }

}

ViewController1:

import UIKit

class ViewController: UIViewController, TransferirTexto {

    @IBOutlet weak var miLabel: UILabel!

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
    }



    func transferir(texto: String) {
        miLabel.text = texto
    }

    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        let svc = segue.destinationViewController as! SecondViewController

        svc.delegate = self

    }

}

How does the issue of delegates work in this case? When changing from one viewController to another, does not the first viewController get destroyed? The question arises of how do I pass it a .delegate = self if the viewController is going to be destroyed?

    
asked by MatiEzelQ 11.04.2016 в 18:37
source

2 answers

1

It's simple, I'll give you an example so you can see how it works.

ViewController 1:

 class ViewController1 : UIViewcontroller, MenuTableProtocol {
            if let menuTable = NSBundle.mainBundle().loadNibNamed("MenuTableView", owner: self, options: nil).first as? MenuTableView {
                let items = ["Item1","Item2"]
                menuTable.items = items
                menuTable.tblDelegate = self //Aqui le estamos diciendo que yo Viewcontroller1 me hago cargo del delegado
                self.view.addSubview(menuTable)
            }

/**
    Aqui implementamos la función del Protocolo que nos interesa manejar
    */
    func didSelect(referenceItem: String) {
        let vc = self.storyboard?.instantiateViewControllerWithIdentifier(referenceItem)
            self.navigationController?.pushViewController(vc!, animated: true)
    }
}

ViewController 2:

    class ViewController2 : UIViewController {
//Lo siguiente sirve para que al instanciar la clase ViewController2 el controlador que quiera se puede hacer cargo de este protocolo
        var tblDelegate:MenuTableProtocol!

        func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath:   NSIndexPath) {
            //Aqui estamos empujando al delegado a que actúe
            self.tblDelegate.didSelect!(items[indexPath.row].reference)
        }

    }

-As you have been told ViewController1 and ViewController2 continue to exist, they are never deleted from the stack, they simply create a connection between them and they communicate, which is what the Protocols are for. Greetings!

    
answered by 20.04.2016 в 11:23
0

First, the ViewController1 is instantiated. From there instances the second in the prepareForSegue. There you have the 2 loaded drivers, none has been destroyed. With the svc.delegate = self you tell the ViewContrller2 how to access the ViewController1 (the delegate does not stop being a pointer to the other controller). From the ViewController2, when you pass the information to the first one using delegate? Transfer (textField.text!), That pointer still points to the ViewController1, so the information arrives and when you do the self.navigationController? .PopViewControllerAnimated (true) is when The ViewController2 is downloaded, but not the ViewController1.

Another important thing you must do is when you declare the delegate do it like this: weak var delegate: TransferText?

Imagine that the delegates are pointed upside down, instead of ViewController2 having a delegate pointing to ViewController1, it is ViewController1 which has a delegate = ViewController2 and at a certain moment you make a popViewControllerAnimated and the ViewController2 disappears. FromViewController1 you still have the delegate pointing to something that no longer exists, and that does not like Swift. When you put it weak, when ViewController2 is destroyed, the delegate stays in nil, so being optional when you execute it does not do anything.

    
answered by 11.04.2016 в 21:58