如何将数据从NSWindowController传递到其NSViewController? [英] How to pass data from NSWindowController to its NSViewController?

查看:264
本文介绍了如何将数据从NSWindowController传递到其NSViewController?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 IBOutlet NSToolBar 按钮在我的 NSWindowController class,这是我的主窗口类:

  class MainWindowController:NSWindowController {

@ IBOutlet weak var myButton:NSButton!

// ...
}

c $> $

如何在我的内容 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
}

}


I have a IBOutlet of a NSToolBar button in my NSWindowController class, which is my main window class:

class MainWindowController: NSWindowController {

    @IBOutlet weak var myButton: NSButton!

    // ...
}

I have a class MainViewController that is that content NSViewController of the main window.

How can I access this button in my content NSViewController? Is there a better way to organize the IBOutlets and the controllers to facilitate this access?

解决方案

How about like this using delegate? This example will change your button's title.

@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
    }

}

这篇关于如何将数据从NSWindowController传递到其NSViewController?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆