简体   繁体   English

在AlerView的操作按钮上将文本设置为TextView,然后单击[iOS,Swift]

[英]set text to TextView on action button of AlerView click [iOS, Swift]

I've created UIAlertController with Three action buttons, now I want to set text to a TextView on each button. 我创建UIAlertController具有三个操作按钮的UIAlertController ,现在我想在每个按钮上将文本设置为TextView Problem is this that when I click a button then result of previously clicked button is printed on TextView . 问题是当我单击一个按钮时,先前单击的按钮的结果会打印在TextView

My Code 我的密码

import UIKit

class ViewController: UIViewController {

    @IBOutlet weak var mTextView: UITextView!

    override func viewDidLoad() {
        super.viewDidLoad()
        mTextView.text = ""
        // Do any additional setup after loading the view, typically from a nib.
    }

    @IBAction func showDialogOnClick(_ sender: UIButton) {

        //Creating UIAlert, giving its title and message, setting style(alert OR alertSheet)
        let mDialog = UIAlertController(title: "Title", message: "This is the message" , preferredStyle: .actionSheet)

        //add actions/buttons to alert
        mDialog.addAction(UIAlertAction(title: "Cancel", style: .cancel, handler: {(action) in
            print("Cancel")
            self.mTextView.text = "Cancel"
        }))
        mDialog.addAction(UIAlertAction(title: "Default", style: .default, handler: {(action) in
            print("Default")
            self.mTextView.text = "Default"
        }))
        mDialog.addAction(UIAlertAction(title: "Destructive", style: .destructive, handler: {(action) in
            print("Destructive")
            self.mTextView.text = "Destructive"
        }))

        self.present(mDialog, animated: true)

    }

}

What i think can be possible issue is you are testing this in your Simulator and your computer is having low graphic configuration same thing happens with be when i test my code in my old mac mini.You can do two things :- 我认为可能的问题是,您正在模拟器中对此进行测试,并且计算机的图形配置较低,当我在旧的Mac mini中测试代码时,也会发生同样的事情。您可以做两件事:-

  1. Test the code in a physical device 在物理设备中测试代码
  2. Try to minimize your app in your simulator and come back to the app you'll see the effect Or change the view back and forth to see the effect. 尝试将模拟器中的应用程序最小化,然后返回该应用程序,您将看到效果,或者来回更改视图以查看效果。

Let me know if I was right. 让我知道我是否正确。

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

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