只使用
Swift,这是我在AppDelegate.swift中的代码:
- import Cocoa
- class AppDelegate: NSObject,NSApplicationDelegate {
- @IBOutlet var window: NSWindow
- @IBOutlet var textField: NSTextView
- @IBAction func displaySomeText(AnyObject) {
- textField.insertText("A string...")
- }
- func applicationDidFinishLaunching(aNotification: NSNotification?) {
- // Insert code here to initialize your application
- }
- func applicationWillTerminate(aNotification: NSNotification?) {
- // Insert code here to tear down your application
- }
- }
在界面构建器中,我有一个对象挂起来从一个按钮接收输入,然后输出到文本视图.当我点击按钮时,我正在尝试使文本视图填充一些文本.
我也尝试了一个文本字段,并没有得到错误,但有一个“东”错误的声音,它没有做任何其他的事情.在Objective-C中,您必须使用(assign)参数才能使其从我所理解的方面工作.
我究竟做错了什么?
使用@IBOutlet var scrollView:NSScrollView而不是@IBOutlet var textField:NSTextView.
然后在scrollView中创建一个返回documentView的属性.
然后在scrollView中创建一个返回documentView的属性.
- import Cocoa
- class AppDelegate: NSObject,NSApplicationDelegate {
- @IBOutlet var window: NSWindow
- @IBOutlet var scrollView: NSScrollView
- var textField: NSTextView {
- get {
- return scrollView.contentView.documentView as NSTextView
- }
- }
- @IBAction func displaySomeText(AnyObject) {
- textField.insertText("A string...")
- }
- func applicationDidFinishLaunching(aNotification: NSNotification?) {
- // Insert code here to initialize your application
- }
- func applicationWillTerminate(aNotification: NSNotification?) {
- // Insert code here to tear down your application
- }
- }