我有一个IBOutlet一个的NSToolBar按钮在我的NSWindowController课,这是我的主要窗口类:
IBOutlet
NSToolBar
NSWindowController
class MainWindowController: NSWindowController { @IBOutlet weak var myButton: NSButton! // ... }
我有一个类MainViewController就是NSViewController主窗口的内容。
MainViewController
NSViewController
如何访问内容中的此按钮NSViewController?有没有更好的方法来组织IBOutlets和控制器以促进此访问?
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 } }