在 Swift 中命名或标记 UITextViews?

Naming or Tagging UITextViews in Swift?

我需要指定一个 textView 我想在其中更新文本。我尝试了以下方法:

func updateView(message: String) {
    var textView2 = UITextView.viewWithTag(2)
    textView2.text = message
}

但我收到此错误:

type 'UIView' does not conform to protocol 'IntegerLiteralConvertable

你做错了。您正在尝试获取 textview 的子视图,这是不正确的。相反,您应该为 UITextView 查询超级视图。应该是:

func updateView(message: String) {
    var textView2 = self.view.viewWithTag(2) as UITextView;
    textView2.text = message
}

在swift中,你必须进行类型转换,而且你必须提供超级视图

 var textView2 : UITextView? = self.view.viewWithTag(2) as? UITextView;

所以你的函数应该是这样的:-

func updateView(message: String) {
    var textView2 : UITextView? = self.view.viewWithTag(2) as? UITextView;
    textView2.text = message
}

如果 viewWithTag() 用于 "find" 情节提要中的组件,了解 IBOutlets 是件好事:

http://codewithchris.com/9-hooking-it-all-up-swift-iboutlet-properties/

2:30左右