如何将数据从NSWindowController传递到其NSViewController? [英] How to pass data from NSWindowController to its NSViewController?
问题描述
我有一个 IBOutlet
的 NSToolBar
按钮在我的 NSWindowController
class,这是我的主窗口类:
class MainWindowController:NSWindowController {
@ IBOutlet weak var myButton:NSButton!
// ...
}
c $> $ 如何在我的内容 如何使用委托?此示例将更改您的按钮的标题。 I have a I have a class How can I access this button in my content How about like this using delegate? This example will change your button's title.
这篇关于如何将数据从NSWindowController传递到其NSViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
NSViewController
中访问此按钮?是否有更好的方法来组织 IBOutlets
和控制器以方便此访问?
@objc protocol SomeDelegate {
func changeTitle(title:String)
}
b $ b 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()
//在窗口控件的窗口加载完成后, nib文件。
let myVc = window!.contentViewController as! ViewController
myVc.delegate = self
}
func changeTitle(title:String){
myButton.title = title
}
}
IBOutlet
of a NSToolBar
button in my NSWindowController
class, which is my main window class:class MainWindowController: NSWindowController {
@IBOutlet weak var myButton: NSButton!
// ...
}
MainViewController
that is that content NSViewController
of the main window.NSViewController
? Is there a better way to organize the IBOutlets
and the controllers to facilitate this access?@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
}
}