Swift: 在 UItextView 中更改文本的按钮?

Swift: a button to change the text in the UItextView?

我正在尝试实现一个按钮来触发我在 UItextView 中的原始文本。我看过这个post: Trigger button to change text in UITextView in swift3 xcode 8 ios 10

跟着它我意识到它只适用于 UItextField。由于我需要包装我的文本,我想知道是否可以用 UItextView 执行类似的操作?

目前,我有:

@IBOutlet var Textfield: UITextView!
@IBOutlet var ChangingText: [UIButton]!


@IBAction func ChangingTextTapped(_ sender: Any) {
        Textfield.text = "Changing text here"
    }

但是我得到以下错误:

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayM setText:]: unrecognized selector sent to instance 0x282d15b90'

提前致谢!

解决方法: 根据 Dejan Atanasov 的建议,它运行良好:

 @IBOutlet var Textfield: UITextView!
    @IBOutlet var ChangingText: UIButton!


    @IBAction func ChangingTextTapped(_ btn: UIbutton) {
            Textfield.text = "Changing text here"
        }`

要通过按下按钮更改 UITextView 文本,您需要以下代码:

@IBOutlet fileprivate weak var textView: UITextView!
@IBOutlet fileprivate weak var changeTextBtn: UIButton!


@IBAction func onChangeTextButton(_ btn: UIButton){
    textView.text = "Change text to something else!"
}

@IBAction 与 Interface Builder 中的按钮连接起来,并确保 select Touch Up Inside 事件(见截图)。而已!

这是我点击按钮时的结果:

您的代码存在以下问题:

@IBOutlet var ChangingText: [UIButton]!

通过将 UIButton 括在方括号中,您已将按钮声明为 UIButton 数组...[UIButton]。只需删除括号。

无需添加按钮数组。

@IBOutlet weak var textViewDemo: UITextView!

@IBAction func upgradeBtnTapped(_ sender: Any) {

textViewDemo.text = "Set Something else"
}