Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change UITextView text by using its tag number to identify the element due for change

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

                                         }

The above code is not flagged as an error on Xcode but doesn't change the UITextView (hellatextview) value upon execution. A textView with a tag number (index) exists but isn't being changed.

Any ideas why it isn't working? Ive had the same issue with UIButtons

like image 415
Khan Luke Avatar asked Feb 08 '18 11:02

Khan Luke


2 Answers

You only need to make sure that the scrollviewxp is the very superview for the view trying to get with tag.

Otherwise, there is absolutely 0 reasons to fail.

like image 177
hasan Avatar answered Oct 04 '22 20:10

hasan


SWIFT 4 Working solutions:

1.

if let hellaTextView = scrollviewxp.subviews.first(where: { $0.tag == index }) as? UITextView {
    hellaTextView.text = "success"
}

or

2.

   for subview in scrollviewxp.subviews
{
   if let hellatextview.text = subview as? UITextView 
     {
         if hellatextview.tag == index {
         hellatextview.text = "success"
                              }
    }
 }
like image 33
tereks Avatar answered Oct 04 '22 21:10

tereks