微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/swift/318881.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐