//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
You only need to make sure that the
scrollviewxp
is the very superview for the view trying to get withtag
.
Otherwise, there is absolutely 0 reasons to fail.
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"
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With