【问题标题】:error passing data between ViewController在 ViewController 之间传递数据时出错
【发布时间】:2015-01-17 12:28:56
【问题描述】:

我有一个奇怪的问题。我有一个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 中禁用了用户交互,但现在我需要启用该选项,因为我想滚动UITextView。但是现在当我在 ViewController 之间传递时,数据没有保存。 (只有颜色不保存)

【问题讨论】:

  • 你的问题有点混乱。您是否在TabViewController 的两个视图控制器上使用UITextView?现在是否已禁用任何一个的用户交互?什么没有更新,是文本、颜色还是两者都有?
  • 1.我只在第一个 ViewController 中使用 UItextField。 2. 现在它已启用 U.I.在两个 V.C. 3.颜色没有更新@vacawama
  • 所以你在ViewController 中有一个UITextField 写入模型,然后你在SecondViewController 中更新一个UITextView
  • 我使用 ViewController 中 UITextView 的数据写入模型。 (我使用 UItextfield 在 UiTextView 中编写),是的,我在 secondVC @vacawama 中更新了 textview
  • ViewControllerUITextView中的文字颜色怎么设置?

标签: ios swift uiviewcontroller uitabbarcontroller uitextview


【解决方案1】:

无论出于何种原因(看起来像一个错误),当在 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
}

【讨论】:

  • @Mala,这适用于我的测试项目。如果这对您有用,请接受我的回答并投票。谢谢。
  • 别担心,明天我会测试你的修复!
猜你喜欢
  • 2016-03-24
  • 2017-01-22
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 2017-12-09
  • 2019-11-08
  • 2017-12-27
  • 1970-01-01
相关资源
最近更新 更多