這讓我瘋狂。函數updateTextView()
被調用,由打印語句驗證,但它沒有在我的視圖控制器中設置標籤,並且標籤的打印語句返回nil,即使它具有默認值集,當應用程序是加載。更令人困惑的是,我設置了一個測試按鈕來單獨調用此函數,當我用test()
調用它時,標籤會正確更新。斯威夫特文本標籤零即使有默認值
class GoalDetailViewController: UIViewController, TextDelegate {
@IBAction func test(sender: AnyObject) {
updateTextView()
}
func updateTextView() {
print(goalSummaryTextBox?.text)
print("delegate called")
self.goalSummaryTextBox?.text = GoalsData.summaryText
print(goalSummaryTextBox?.text)
}
@IBOutlet weak var goalTitle: UILabel?
@IBOutlet weak var goalCreationDate: UILabel?
@IBOutlet weak var goalSummaryTextBox: UITextView?
override func viewDidLoad() {
super.viewDidLoad()
goalSummaryTextBox?.text = GoalsData.summaryText
}
}
updateTextView()
正在通過委託方法稱爲後我彈出一個不同的視圖控制器,如在下面可以看到:
class TextEditViewController: UIViewController {
var textDelegate: TextDelegate?
@IBOutlet weak var textView: UITextView?
func configureView() {
navigationItem.title = "Edit Description"
navigationItem.setRightBarButtonItem((UIBarButtonItem(barButtonSystemItem: .Done, target: self, action: "segue")), animated: true)
}
func segue() {
textDelegate = GoalDetailViewController()
if let text = textView?.text {
GoalsData.summaryText = text
}
textDelegate?.updateTextView()
self.navigationController?.popViewControllerAnimated(true)
}
}
最後那句話可能是問題。 – tktsubota
能否詳細說明一下?由於打印語句我知道它調用函數。 –
你可以發佈與彈出的不同視圖控制器有關的代碼(並觸發'updateTextView()'調用)嗎?這可能是'updateTextView()'在後臺隊列上被調用。你還可以將'print(GoalsData.summaryText)'添加到'updateTextView()'來確保它確實不同嗎? – Mark