SWIFT:大炮插入值NSTextView从视图控制器

问题描述:

我创建的文件ViewController.swiftSWIFT:大炮插入值NSTextView从视图控制器

这里是它的内容

import Cocoa 

public class ViewController: NSObject { 

    @IBOutlet var textView: NSTextView! 

    func InsertText(string:String) {   
      textView.insertText(string) 
    } 
} 

然后我绑定的TextView我NSTextView在我的厦门国际银行

现在我想通过控制器从AppDelegate插入文本

我做

let controller = ViewController() 
controller.InsertText("Hello") 

,它抛出一个错误

fatal error: unexpectedly found nil while unwrapping an Optional value 

这意味着在TextView的零,但为什么它是零,我怎么使用另一个类插入文本。如果我只是在AppDelegate中做同样的工作。

而不是let controller = ViewController() ctrl-将视图拖动到您的AppDelegate中,将其命名合理并使用它。

+0

编辑,哦,工程,第二次尝试:D谢谢 – user840250 2014-12-07 15:46:56

+0

你在哪里实例化ViewController?我想你有点混淆了。通常你为NSTextView创建一个ViewController,并将它绑定到XIB中。然后你可以使用AppDelegate。 – 2014-12-07 15:52:12