![](/img/trans.png)
[英]Swift - insert item in TableView from another ViewController
[英]SWIFT: cannon insert value to NSTextView From ViewController
我創建了文件ViewController.swift
這是它的內容
import Cocoa
public class ViewController: NSObject {
@IBOutlet var textView: NSTextView!
func InsertText(string:String) {
textView.insertText(string)
}
}
然后我在我的xib中將textView綁定到我的NSTextView
和
現在我想通過控制器從AppDelegate插入文本
我做
let controller = ViewController()
controller.InsertText("Hello")
它拋出一個錯誤
fatal error: unexpectedly found nil while unwrapping an Optional value
這意味着textView為nil,但為什么為nil以及如何使用另一個類插入文本。 如果我只是在AppDelegate中做同樣的事情,它將起作用。
與其let controller = ViewController()
ctrl拖動視圖到AppDelegate中,不如將其命名為合理並使用它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.