【问题标题】:SWIFT: cannon insert value to NSTextView From ViewControllerSWIFT:大炮从 ViewController 向 NSTextView 插入值
【发布时间】:2014-12-07 15:39:59
【问题描述】:

我创建了文件 ViewController.swift

这是它的内容

import Cocoa

public class ViewController: NSObject {

    @IBOutlet var textView: NSTextView!

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

然后我将 textView 绑定到我的 xib 中的 NSTextView

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

我愿意

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

会报错

fatal error: unexpectedly found nil while unwrapping an Optional value

这意味着 textView 在 nil,但为什么它是 nil 以及如何使用另一个类插入文本。如果我只是在 AppDelegate 中做同样的事情。

【问题讨论】:

    标签: swift


    【解决方案1】:

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

    【讨论】:

    • 你在哪里实例化 ViewController?估计你有点混通常,您为 NSTextView 创建一个 ViewController 并将其绑定到 XIB 中。然后你可以从 AppDelegate 使用它。
    猜你喜欢
    • 2019-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多