通过使用其标记号更改UITextView文本以标识要更改的元素

Kha*_*uke 7 uitextview ios swift

//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"
                        }

                                         }
Run Code Online (Sandbox Code Playgroud)

上面的代码在Xcode上没有被标记为错误,但在执行时不会更改UITextView(hellatextview)值.带有标签号(索引)的textView存在但未更改.

任何想法为什么它不起作用?我和UIButton有同样的问题

has*_*san 4

您只需要确保 是scrollviewxp试图使用 的视图的超级视图tag

否则的话,失败的理由绝对是0。