//UITextView Creation
let textarea = UITextView (frame : CGRect (x: 40, y: 100 ,width:100 , height:100))
textarea.delegate = self
textarea.tag = self.numarr
textarea.backgroundColor = UIColor(red: 0.9686, green: 0.9686, blue: 0.9686, alpha: 1.0)
textarea.layer.cornerRadius = 20.0
textarea.contentInset = UIEdgeInsetsMake(5, 5, 5, 5);
textarea.layer.masksToBounds = true
self.scrollviewxp.addSubview(textarea)
//Later after, the button function
@IBAction func loadbutton(_ sender: Any) {
if let hellatextview = self.scrollviewxp.viewWithTag(index) as? UITextView
{
hellatextview.text = "success"
}
}
上述代码未在Xcode上标记为错误,但在执行时不会更改UITextView(hellatextview)值。带有标签号(索引)的textView存在但未更改。
为什么它不起作用的任何想法?我对UIButtons有同样的问题
答案 0 :(得分:4)
您只需要确保
scrollviewxp
就是这样 尝试使用tag
获取视图的超级视图。
否则,绝对有0个理由失败。
答案 1 :(得分:4)
SWIFT 4工作解决方案:
1
if let hellaTextView = scrollviewxp.subviews.first(where: { $0.tag == index }) as? UITextView {
hellaTextView.text = "success"
}
或
2
for subview in scrollviewxp.subviews
{
if let hellatextview.text = subview as? UITextView
{
if hellatextview.tag == index {
hellatextview.text = "success"
}
}
}