Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delegate Not Called In SWIFT iOS

Tags:

ios

swift

I am Created 2 Views, One is and Used Protocol and Delegate. For first view the Delegate function is not called.

My FirstView Controller : Here I am Accessing the Delegate Function.

import UIKit

class NextViewController: UIViewController,DurationSelectDelegate {
    //var secondController: DurationDel?

    var secondController: DurationDel = DurationDel()

    @IBAction func Next(sender : AnyObject)
    {
        let nextViewController = DurationDel(nibName: "DurationDel", bundle: nil)
        self.navigationController.pushViewController(nextViewController, animated: true)
    }


    override func viewDidLoad() {
        super.viewDidLoad()

        secondController.delegate=self
    }

    func DurationSelected() {
        println("SUCCESS")
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}

My SecondView Controller : Here I Am creating Delegate.

import UIKit

protocol DurationSelectDelegate {
    func DurationSelected()
}


class DurationDel: UIViewController {

    var delegate: DurationSelectDelegate?

    @IBAction func Previous(sender : AnyObject) {
        //let game = DurationSelectDelegate()
        delegate?.DurationSelected()
        self.navigationController.popViewControllerAnimated(true)
    }


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

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }

}
like image 297
PREMKUMAR Avatar asked Dec 26 '22 08:12

PREMKUMAR


1 Answers

To me, it looks like you're pushing a view controller that you haven't actually set the delegate for. If you change your "Next" function, to include the line

nextViewController.delegate = self

You should see that the delegation works. In doing this, you can also probably remove the creation of "secondController", as it looks like that's redundant.

like image 75
Eagerod Avatar answered Jan 08 '23 18:01

Eagerod