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

前端之家收集整理的这篇文章主要介绍了swift – 不能形成对类NSTextView的实例的弱引用前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
只使用 Swift,这是我在AppDelegate.swift中的代码
  1. import Cocoa
  2.  
  3. class AppDelegate: NSObject,NSApplicationDelegate {
  4.  
  5. @IBOutlet var window: NSWindow
  6. @IBOutlet var textField: NSTextView
  7.  
  8. @IBAction func displaySomeText(AnyObject) {
  9. textField.insertText("A string...")
  10. }
  11.  
  12. func applicationDidFinishLaunching(aNotification: NSNotification?) {
  13. // Insert code here to initialize your application
  14. }
  15.  
  16. func applicationWillTerminate(aNotification: NSNotification?) {
  17. // Insert code here to tear down your application
  18. }
  19.  
  20.  
  21. }

在界面构建器中,我有一个对象挂起来从一个按钮接收输入,然后输出到文本视图.当我点击按钮时,我正在尝试使文本视图填充一些文本.

我也尝试了一个文本字段,并没有得到错误,但有一个“东”错误的声音,它没有做任何其他的事情.在Objective-C中,您必须使用(assign)参数才能使其从我所理解的方面工作.

我究竟做错了什么?

使用@IBOutlet var scrollView:NSScrollView而不是@IBOutlet var textField:NSTextView.
然后在scrollView中创建一个返回documentView的属性.
  1. import Cocoa
  2.  
  3. class AppDelegate: NSObject,NSApplicationDelegate {
  4.  
  5. @IBOutlet var window: NSWindow
  6. @IBOutlet var scrollView: NSScrollView
  7.  
  8. var textField: NSTextView {
  9. get {
  10. return scrollView.contentView.documentView as NSTextView
  11. }
  12. }
  13.  
  14. @IBAction func displaySomeText(AnyObject) {
  15. textField.insertText("A string...")
  16. }
  17.  
  18. func applicationDidFinishLaunching(aNotification: NSNotification?) {
  19. // Insert code here to initialize your application
  20. }
  21.  
  22. func applicationWillTerminate(aNotification: NSNotification?) {
  23. // Insert code here to tear down your application
  24. }
  25. }

猜你在找的Swift相关文章