代表没有被设定

时间:2014-08-30 09:25:46

标签: delegates swift

我有一个类Document(NSDocument的子类)和一个属性(称为documentString),当它被更改时,应该调用它的委托。在我的例子中,委托是一个ViewController。虽然委托是在ViewController中设置的,但它似乎是nil并且ViewController应该响应的函数没有被调用

这是属性,应将其更改报告给代理人(始终打印"否")。

var documentString: String {
    didSet {
        if self.delegate != nil {
            self.delegate?.documentContentDidChange()
        } else {
            println("no")
        }
    }
}

这是ViewController。

class ViewController: NSViewController, DocumentDelegate {

    @IBOutlet var codeView: NSTextView!
    var code: Document = Document()

    override func viewDidLoad() {
        super.viewDidLoad()
        self.code.delegate = self
        self.updateUI()                            
    }

    func documentContentDidChange() {
        self.updateUI()
    }

    func updateUI() {
        self.codeView.string = self.code.documentString
    }
}

我做错了什么?

编辑:我在println("no1")之后添加了self.code.delegate = self。没有在no1之前打印。因此,这意味着在更改documentString时未设置委托。但是,updateUI应该与在documentString中显示文本的codeView一起正常工作。但是,codeView仍为空白。

0 个答案:

没有答案