Delegate in Swift-language

i0S Swift Issue

Question or problem in the Swift programming 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")
        }

But function not getting called

How to solve the problem:

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;
    }
}

Hope this helps!