ios - Swift 协议(protocol)委托(delegate)返回 nil

标签 ios iphone swift3

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/

相关文章:

ios - uiscrollview 中的 UIViewControllers 调用 viewdidappear

ios - UIView --> 不可见 View 的 UIImage?

ios - UIView 方法 swizzling swift 3

iphone - 如何使用 UIScrollView 捕捉缩放?

iphone - iPad 3 和 NSUserDefaults 问题

ios - 如何在iOS Swift中使用不同类的选择器函数,主要用于NotificationCenter

swift - 预填充 .sqlite 的核心数据 (Swift3)

objective-c - 使用 Grand Central Dispatch 设置变量不可检索

android - 我如何将域放在 AWS ESB 前面

ios - iPhone 事件指示器