在 Swift 中从两个 NSWindows 传递变量

Passing variables from two NSWindows in Swift

我正在尝试从 NSWindowController 更新 NSViewController 中的标签。

这是我所做的。

创建协议:

protocol PopoverProtocol: class {
    func SearchForIt(Query:String)
}

创建了一个函数来更新 NSViewController 中的标签

class PopoverController: NSViewController, PopoverProtocol {

    @IBOutlet weak var Label: NSTextField!

    func SearchForIt(Query:String){
            Label.stringValue = Query
    }
}

从 NSWindowController 调用了协议和函数

class TutorialViewController: NSWindowController, NSSearchFieldDelegate {

    weak var responder : PopoverProtocol?

    @IBAction override func controlTextDidChange(obj: NSNotification) {

    PopoVer.showRelativeToRect(SearchField.bounds, ofView: SearchField, preferredEdge: NSRectEdge.MinY)

    responder?.SearchForIt(SearchField.stringValue)

    }
}

然而什么也没有发生。我没有收到任何错误消息,只是没有调用函数 SearchForIt

对我做错了什么有什么想法吗?感谢您的帮助!

协议被 class 采用,就像您在上面的第二个代码块中所做的那样。现在您的视图控制器必须实现 SearchForIt 方法,然后您的视图控制器才能接管调用,而不是单独定义的对象。

在您的 controlTextDidChange 中,将 responder 替换为您的 viewcontroller 实例。