簡體   English   中英

在ViewController之間傳遞數據時出錯

[英]error passing data between ViewController

我有一個奇怪的問題。 我有一個可以寫在里面的UITextField ,文本直接顯示在UITextView 我正在使用UITabBarControllerUIViewController之間切換。 我正在使用該類來保存數據(寫在TabBarController.swift ):

public class ModelData {

    var text = ""
    var color:UIColor? = nil

}

該代碼保存數據(寫在ViewController.swift ):

override func viewDidDisappear(animated: Bool) {
    let model = (self.tabBarController as TabBarViewController).model
    model.color = textview.textColor
    model.text = textview.text
}

以及提供數據的代碼(寫在SecondViewController.swift ):

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(animated)
    let model = (self.tabBarController as TabBarViewController).model

    textview.textColor = model.color
    textview.text = model.text

}

所以,我的問題是:因為我使用可以在其中寫的UItextField ,所以已禁用UITextView User Interaction,但是現在我需要啟用該選項,因為我想滾動UITextView 但是現在當我在ViewController之間傳遞時,數據沒有保存。 (僅不保存顏色)

無論出於什么原因(看起來像個錯誤),當在Interface Builder中取消選中Selectable時, UITextViewtextColor屬性都將返回nil 如果將selectable屬性在代碼中設置為false ,則沒有問題。 因此,一種解決方法是在Interface Builder中將Selectable選中狀態,然后在viewDidLoad中將其設置為false

override func viewDidLoad() {
    super.viewDidLoad()

    // Set selectable to false here so that the user can't select the
    // textview text, but User Interaction is still enabled to allow them
    // to scroll it.  This is a workaround for a problem in Interface
    // Builder which causes the textColor property to return nil if Selectable
    // is unchecked in Interface Builder.
    textview.selectable = false
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM