简体   繁体   中英

Swift Protocol Delegate return nil

protocol testDelegate: class {
    func open(channel: String, vc: UIViewController)
}

class test: UIViewController{

    weak var delegate: testDelegate?
}
override func viewDidLoad() {
        super.viewDidLoad()
        if self.delegate != nil {
        print("hello")
        self.delegate?.openGroupChannel(channel: channel!, vc: self)

}   

that is Class Test! protocol init in Test class as well

class calling:testDelegate{

override func viewDidLoad() {
        //blah blah       
    }   

func func open(channel: String, vc: UIViewController){
    print("calling")
}

This is calling class.

I want to call open func in calling class but it does not calling at all, even print("hello") in test class is not calling it keeps return nil therefore does not call calling function as well.

You need to set your calling as delegate of test ViewController. In your calling class create object of test class before navigation and set calling class as delegate of your test class as

override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let testVc = segue.destinationViewController as? test {
    testVc .delegate = self
    }
}

Hope it helps.. Happy Coding!!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM