我以编程方式创建了一个文本视图,并且在viewdidload之外有一个外部函数,我想在其中更改该文本视图的内容。我如何实现这一目标?这里是我正在使用的代码 就像事情一样。我目前收到这个错误“Value of type'UIView'has no member'textView''on line
self.view.textView.text =”Yo Dawg !!“如何从一个calss成员函数访问编程创建的UI对象
class rootViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
let myTextView = UITextView(frame: CGRectMake(0,0,100,50))
myTextView.backgroundColor = UIColor.clearColor()
myTextView.textColor = UIColor.whiteColor()
myTextView.font = UIFont (name: "Helvetica Neue", size: 20)
myTextView.textAlignment = NSTextAlignment.Center
myTextView.text = "Hello World"
self.view.addSubview(timerView)
}//End view did load
func changeText(){//This function updates the text within the textview
self.view.textView.text = "Yo Dawg!!"
}//End change text function
}//End rootViewController
Paulw11我明白你的推理,但这是行不通的。我得到这个错误“类型rootViewController的成员没有成员myTextView”上面Seto提供的解决方案,但是,作品 –
检查你的输入。 – Paulw11
IIRC,你不需要用'self.'来快速调用变量。因此,不用调用'self.myTextView.text =',你可以简单地写'myTextView.text ='。 –