Swift: 如何将 class 的委托分配给当前 class 中的另一个 class?

Swift: How to assign a class's delegate to another class in current class?

protocol TestDelegate {
    func toggleLeftPanel()
}
class A: UIViewController, TestDelegate {
    //...do sth.
    func toggleLeftPanel() {
        //do sth.
    }
    //...do sth.
}
class B: UIViewController {
    var delegate: TestDelegate?
    func onMenu() {
        delegate?.toggleLeftPanel()
    }
}
class C: UIViewController {
    func presentAction() {
        let b = b()
        b.delegate = A.self//Here will report an error

        let b = B()// I got the instance of B controller
        presentViewController(b, animated: true, completion: nil)
    }
}

认为我现在在C UIViewController(这个对应IPhone中的一个screen),然后我会通过点击一个按钮(presentAction)去到B UIViewController。
但是当我到达B UIViewController时,我发现onMenu不起作用(toggleLeftPanel不起作用),因为b的委托属性为nil,所以我决定给它分配一个class(实例?)( b.delegate ) before present b UIViewController, 但我得到一个错误 "cannot assign a value of type 'xxxController.Type' to a value of type'xxxDelegate?'".
我该如何解决这个问题?或者我应该再次在 class C 中实现 TestDelegate 并将 b.delegate 分配给它自己?

let b = b()
b.delegate = A.self

上面的代码完全是胡说八道。首先,没有class名字"b"。应该令 b = B()。其次,您应该分配一个实例来委托。不是 class。像 b.delegate = self 这样的东西。请阅读https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

您也可以试试下面的代码

protocol TestDelegate: class {
    func toggleLeftPanel()
}

class A: UIViewController, TestDelegate {
    //...do sth.
    func toggleLeftPanel() {
        //do sth.
    }
    //...do sth.
}

class B: UIViewController {
    weak var delegate: TestDelegate?
    func onMenu() {
        delegate?.toggleLeftPanel()
    }
}

class C: UIViewController {
    func presentAction() {

        let a = A()
        let b = B()
        b.delegate = a
        presentViewController(b, animated: true, completion: nil)
    }
}