单击按钮后如何更改变量 UITextView?

How to change a variable UITextView after clicking the button?

我用随机标签和文本创建 UITextView,但它是用一个变量创建的,是否可以在创建 UITextView 后更新变量(通过单击添加按钮)?也许给它添加一个随机数,例如 newText1、newText2.. 等等

所以下一个 UITextView 已经用新变量创建了?

P.S 不好意思,问题有点傻,我最近才开始研究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       
       }

更新:

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

在键盘上方的工具栏中我有按钮,这里我们更改颜色

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

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

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

所以颜色只在新创建的UITextView

点击按钮,创建一个新的 texView 并为其分配一个标签值。添加完成后,将i的值更新为+1,这样每一个添加的textView都有一个新的tag值。

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
}

然后您可以像这样通过标签值访问您的 textField:

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

更新:

增加textView的Delegate方法,当一个textView开始编辑时,将newText的值改为当前正在编辑的textView

class ViewController : UIViewController, UITextViewDelegate {

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

我已经稍微修改了您的代码,使新的 UITextView 对象带有按钮点击

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

它将有这样的输出!!