Dmitry
Dmitry

Reputation: 25

How to change a variable UITextView after clicking the button?

I create UITextView with a random tag and text, but it is created with one variable, is it possible to update the variable after creation UITextView (by clicking the add button)? Maybe add a random number to it, for example newText1, newText2.. etc.

So that the next UITextView is already created with a new variable?

P.S Sorry, if the question is silly, I just recently started to study Swift

@IBOutlet weak var addTextButton: UIButton!
@IBOutlet weak var StoriesView: UIView!

var newText = UITextView()

override func viewDidLoad() {
        super.viewDidLoad()
}

@IBAction func addTextButton(_ sender: Any) {

        let maxNumber = 10000
        let i = Int(arc4random_uniform(UInt32(maxNumber)))

        newText = UITextView(frame: CGRect(x: self.StoriesView.frame.origin.x + 40, y: self.StoriesView.frame.origin.y + 40, width: 380, height: 80))
         self.StoriesView.addSubview(newText)

         newText.font = UIFont(name: "Verdana", size: 11)
         newText.text = "TAP TO EDIT #\(i)"
         newText.sizeToFit()
         newText.textColor = UIColor.black
         newText.backgroundColor = UIColor.clear
         newText.tag = i

         newText.isEditable = true
         newText.isSelectable = true
         newText.isUserInteractionEnabled = true
         newText.allowsEditingTextAttributes = true
         newText.translatesAutoresizingMaskIntoConstraints = true

         newText.enablesReturnKeyAutomatically = true

         newText.delegate = self       
       }

UPD:

let fontToolbar = UIToolbar(frame:CGRect(x: 0, y: 0, width: UIScreen.main.bounds.width, height: 50))
        fontToolbar.barStyle = .default
        fontToolbar.items = [
            UIBarButtonItem(title: "Green", style: .plain, target: self, action: #selector(greenColor)),
             UIBarButtonItem(title: "Blue", style: .plain, target: self, action: #selector(blueColor)),
             UIBarButtonItem(title: "Red", style: .plain, target: self, action: #selector(redColor)),
        UIBarButtonItem(barButtonSystemItem: .flexibleSpace, target: nil, action: nil),
        UIBarButtonItem(title: "Close Keyboard", style: .plain, target: self, action: #selector(dismissKeyboard))]
        fontToolbar.sizeToFit()
        newText.inputAccessoryView = fontToolbar

in the toolBar above the keyboard I have buttons, here we change the color

@objc func redColor() {
        newText.textColor = UIColor.red}

    @objc func blueColor() {
        newText.textColor = UIColor.blue}

    @objc func greenColor() {
        newText.textColor = UIColor.green}

So the color changes only in the newly created UITextView

Upvotes: 1

Views: 169

Answers (2)

RJ168
RJ168

Reputation: 1046

I have modified your code a bit to have new UITextView object with button click

import UIKit

class ScannerViewController: UIViewController, UITextViewDelegate {

  @IBOutlet weak var StoriesView: UIView!
  @IBOutlet weak var addTextButton: UIButton!
  var yposition: CGFloat!
  var textFieldTag: [Int]! = []

    override func viewDidLoad() {
    super.viewDidLoad()
    yposition = 20
  }

  @IBAction func addTextButton(_ sender: Any) {

    let xposition = self.StoriesView.frame.origin.x
    let maxNumber = 10000
    let i = Int(arc4random_uniform(UInt32(maxNumber)))
    textFieldTag.append(i)

    let newText = UITextView(frame: CGRect(x: xposition , y: yposition , width: 380, height: 40))
    self.StoriesView.addSubview(newText)

    newText.font = UIFont(name: "Verdana", size: 11)
    newText.text = "TAP TO EDIT #\(i)"
    newText.sizeToFit()
    newText.textColor = UIColor.black
    newText.backgroundColor = UIColor.yellow
    newText.tag = i

    newText.isEditable = true
    newText.isSelectable = true
    newText.isUserInteractionEnabled = true
    newText.allowsEditingTextAttributes = true
    newText.translatesAutoresizingMaskIntoConstraints = true

    newText.enablesReturnKeyAutomatically = true
    newText.delegate = self

    yposition = yposition + 45
  }

  @IBAction func accessTextFields(_ sender: Any) {

    //access all text fields
    for tag in textFieldTag {
      if let textField = self.StoriesView.viewWithTag(tag) as? UITextView {
        //change properties here
        textField.backgroundColor = .cyan
      }
    }

    //access specific text fields
    if let textField = self.StoriesView.viewWithTag(textFieldTag.first!) as? UITextView {
      //change properties here
      textField.backgroundColor = .orange
    }

    if let textField = self.StoriesView.viewWithTag(textFieldTag[textFieldTag.count - 1]) as? UITextView {
      //change properties here
      textField.backgroundColor = .green
    }
  }
}

It will have an output as this!!

enter image description here

Upvotes: 1

Keshu R.
Keshu R.

Reputation: 5225

On click of button, create a new texView and assign it a tag value. Once it is added, update the value of i to +1, so that every textView added has a new tag value.

var i = 1
var newText = UITextView()

@IBAction func addTextButton(_ sender: Any) {

    newText = UITextView(frame: CGRect(x: self.StoriesView.frame.origin.x + 40, y: self.StoriesView.frame.origin.y + 40, width: 380, height: 80))
     self.StoriesView.addSubview(newText)

    newText.font = UIFont(name: "Verdana", size: 11)
    newText.text = "TAP TO EDIT #\(i)"
    newText.sizeToFit()
    newText.textColor = UIColor.black
    newText.backgroundColor = UIColor.clear
    newText.tag = i

    newText.isEditable = true
    newText.isSelectable = true
    newText.isUserInteractionEnabled = true
    newText.allowsEditingTextAttributes = true
    newText.translatesAutoresizingMaskIntoConstraints = true

    newText.enablesReturnKeyAutomatically = true

    newText.delegate = self
    //increment i
    i+=1
}

then you can access your textField via tag values like this:

if let textView = self.StoriesView.viewWithTag(i) as? UITextView {
     // textView.text = "change it"   
}

UPDATE:

Add textView Delegate method, and once a textView starts editing, change the newText value to the currently editing textView

class ViewController : UIViewController, UITextViewDelegate {

    func textViewDidBeginEditing(_ textView: UITextView) {
        newText = textView
    }
}

Upvotes: 2

Related Questions