简体   繁体   English

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

[英]Swift: a button to change the text in the UItextView?

I am trying to implement a button to trigger the original text I have in the UItextView.我正在尝试实现一个按钮来触发我在 UItextView 中的原始文本。 I have looked at this post: Trigger button to change text in UITextView in swift3 xcode 8 ios 10我看过这篇文章: Trigger button to change text in UITextView in swift3 xcode 8 ios 10

and followed it I realized it only worked for UItextField.然后我意识到它只适用于 UItextField。 Since I need to wrap the my text, I am wondering is that possible to do the similar action with UItextView?由于我需要包装我的文本,我想知道是否可以使用 UItextView 执行类似的操作?

Currently, I have:目前,我有:

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


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

But I got the following error:但我收到以下错误:

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

Thanks in advance!提前致谢!

Solution: It works well following the suggestion from Dejan Atanasov:解决方案:按照 Dejan Atanasov 的建议,它运行良好:

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


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

For changing the UITextView text by pressing on a button you will need the following code:要通过按下按钮更改 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!"
}

Connect the @IBAction with the button in the Interface Builder and make sure you select the Touch Up Inside event (see screenshot).@IBAction与 Interface Builder 中的按钮连接起来,并确保 select Touch Up Inside事件(见屏幕截图)。 That's it!而已!

在此处输入图像描述

This is the result when I click on the button:这是我单击按钮时的结果:

在此处输入图像描述

No need to add array of buttons.无需添加按钮数组。

@IBOutlet weak var textViewDemo: UITextView!

@IBAction func upgradeBtnTapped(_ sender: Any) {

textViewDemo.text = "Set Something else"
}

The Issue with your code was the following line:您的代码的问题是以下行:

@IBOutlet var ChangingText: [UIButton]!

Your have declared your button as an array of UIButtons by enclosing UIButton in Brackets...[UIButton].您已通过将 UIButton 括在括号中来将您的按钮声明为 UIButtons 数组...[UIButton]。 Simply remove the brackets.只需卸下支架。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM