繁体   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)

}   

那是课堂测试! 测试类中的协议初始化

class calling:testDelegate{

override func viewDidLoad() {
        //blah blah       
    }   

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

这是上课。

我想在调用类中调用open func,但它根本不调用,即使测试类中的print(“ hello”)都没有调用,它始终返回nil,因此也不会调用调用函数。

您需要将calling设置为test ViewController的委托。 在导航之前,在您的calling类中创建test类的对象,并将calling类设置为您的test类的委托为

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

希望对您有所帮助。

暂无
暂无

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

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