//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
3 Answers
Answers 1
The following is a great and straight-forward solution based on an answer found here: Swift: How can I detect elements inside of a UIView?
for subview in scrollviewxp.subviews { if let hellatextview.text = subview as? UITextField { if hellatextview.tag == index { hellatextview.text = "success" } } }
Answers 2
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? UITextField { if hellatextview.tag == index { hellatextview.text = "success" } } }
Answers 3
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.
0 comments:
Post a Comment