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)测试!测试类中的协议(protocol)初始化
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 的委托(delegate)。
在您的 calling
类中,在导航之前创建 test
类的对象,并将 calling
类设置为您的 test
类的委托(delegate)
override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
if let testVc = segue.destinationViewController as? test {
testVc .delegate = self
}
}
希望对您有所帮助.. 编码愉快!!
关于ios - Swift 协议(protocol)委托(delegate)返回 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44895419/