我的NSWindowController类中有一个NSToolBar按钮的IBOutlet,这是我的主窗口类:
class MainWindowController: NSWindowController { @IBOutlet weak var myButton: NSButton! // ... }
我有一个类MainViewController,它是主窗口的内容NSViewController.
如何在我的内容NSViewController中访问此按钮?有没有更好的方法来组织IBOutlets和控制器来促进这种访问?
这样使用委托怎么样?此示例将更改按钮的标题.
@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 } }