简体   繁体   English

Swift协议委托返回nil

[英]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. 我想在调用类中调用open func,但它根本不调用,即使测试类中的print(“ hello”)都没有调用,它始终返回nil,因此也不会调用调用函数。

You need to set your calling as delegate of test ViewController. 您需要将calling设置为test ViewController的委托。 In your calling class create object of test class before navigation and set calling class as delegate of your test class as 在导航之前,在您的calling类中创建test类的对象,并将calling类设置为您的test类的委托为

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

Hope it helps.. Happy Coding!! 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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