swift – 不能形成对类NSTextView的实例的弱引用

只使用 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的属性.
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
    }
}

相关文章

软件简介:蓝湖辅助工具,减少移动端开发中控件属性的复制和粘...
现实生活中,我们听到的声音都是时间连续的,我们称为这种信...
前言最近在B站上看到一个漂亮的仙女姐姐跳舞视频,循环看了亿...
【Android App】实战项目之仿抖音的短视频分享App(附源码和...
前言这一篇博客应该是我花时间最多的一次了,从2022年1月底至...
因为我既对接过session、cookie,也对接过JWT,今年因为工作...