您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

如何将数据从NSWindowController传递到其NSViewController?

如何将数据从NSWindowController传递到其NSViewController?

这样使用委托怎么样?本示例将更改按钮的标题

@objc protocol SomeDelegate {
    func changeTitle(title: String)
}

class ViewController: NSViewController {

    weak var delegate: SomeDelegate?

    @IBAction func myAction(sender: AnyObject) {
        delegate?.changeTitle("NewTitle")
    }

}

class MainWindowController: NSWindowController, SomeDelegate {

    @IBOutlet weak var myButton: NSButton!

    override func windowDidLoad() {
        super.windowDidLoad()

        // Implement this method to handle any initialization after your window controller's window has been loaded from its nib file.
        let myVc = window!.contentViewController as! ViewController
        myVc.delegate = self

    }

    func changeTitle(title: String) {
        myButton.title = title
    }

}
其他 2022/1/1 18:15:48 有512人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶