gpt4 book ai didi

ios - 如何通过使用 Swift 单击按钮以编程方式将文本输入 UITextView

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:54:22 25 4
gpt4 key购买 nike

我有一个 UITextView 和两个 UIButton(ButtonOneButtonTwo)。我想通过单击 ButtonOneButtonTwo 在此 UITextView 中键入文本。

例如,如果我单击 ButtonOne,它应该在 UITextView 中键入“apple”,然后如果我单击 ButtonTwo,它应该键入“橙色”进入 UITextView。因此,在我依次单击这两个按钮后,我的 UITextView 中应该有以下文本:

apple orange

我目前实际上可以通过使用以下代码行来做到这一点:

myTextView.text = myTextView.text.stringByAppendingString("orange ")

但是问题是,当我在 myTextView 中键入一些文本后,如果我手动将一些文本输入到 myTextView(使用键盘),然后单击 ButtonOne(或 ButtonTwo ) 再次,所有手动输入的文本都从 myTextView 中删除,我只剩下通过单击按钮输入的文本。

我希望能够通过单击按钮以及交替使用键盘输入来输入文本。

我可以在 .NET 应用程序中使用 RichTextBox 控件非常轻松地完成此类操作。因此,我在这方面遇到的一个相关问题是什么与 iOS 中的 .NET RichTextBox 控件等效。

最佳答案

尝试使用以下代码

@IBOutlet weak var yourTextView: UITextView!

@IBAction func appleTap(sender: AnyObject) {
yourTextView.text = yourTextView.text .stringByAppendingString("Apple ")
}

@IBAction func OrangeTap(sender: AnyObject) {
yourTextView.text = yourTextView.text .stringByAppendingString("Orange ")
}

上面的代码对我有用,如果你还有问题,请在问题编写你的代码

关于ios - 如何通过使用 Swift 单击按钮以编程方式将文本输入 UITextView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35826571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com