Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate in Swift-language

I have two controllers and i need call up function the first controller to second controller: In second controller I have created protocol and init delegate in class:

    protocol testProtocol {
        func testDelegate() // this function the first controllers
    }

    class SecondViewController: UIViewController {
        var delegate: testProtocol?
    ....
    }
    @IBAction func testDelegateClicked(sender : AnyObject) {
            delegate?.testDelegate()
        }

First Controller

        class ViewController: UIViewController, testProtocol {

        var secondController: SecondViewController = SecondViewController()

        override func viewDidLoad() {
            super.viewDidLoad()

            secondController.delegate = self
        }
        func testDelegate() {
            println("Hello delegate")
        }</pre>

But function not getting called

like image 594
Lola Avatar asked Jun 05 '14 11:06

Lola


People also ask

What is delegate in iOS with example?

What is delegate methods in iOS? It is an easy and influential pattern in which one object in a program works on behalf of or in coordination with, another object. The delegating object keeps a reference to the other object and at the suitable time sends a message to it.

What is delegate in Swift medium?

Delegate (verb) — entrust (a task or responsibility) to another person. In programming terms this quote translates to entrust (a task or responsibility) to another object. The delegation uses objects composition to allow customization and code reuse, and stands as an alternative to inheritance.

What is delegate and protocol in iOS?

Delegates are a use of the language feature of protocols. The delegation design pattern is a way of designing your code to use protocols where necessary. In the Cocoa frameworks, the delegate design pattern is used to specify an instance of a class which conforms to a particular protocol.


1 Answers

I am going to make an assumption you are using storyboards. If I am correct, then your issue is that your secondController, created in your First Controller, is not the actual one you are presenting. You will need to set secondController in your prepareForSegue:

Second Controller

Unchanged

First Controller

class ViewController: UIViewController, testProtocol {

    // you will want to add the ? since this variable is now optional (i.e. can be nil)
    var secondController: SecondViewController? // don't assign it a value yet

    // ...

    // implementation of the protocol
    func testDelegate() {
        println("Hello delegate")
    }

    // your prepare for segue
    override func prepareForSegue(segue: UIStoryboardSegue?, sender: AnyObject?) {
        // get the controller that storyboard has instantiated and set it's delegate
        secondController = segue!.destinationViewController as? SecondViewController
        secondController!.delegate = self;
    }
}
like image 145
Firo Avatar answered Sep 20 '22 15:09

Firo