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

时间:2019-02-16 12:17:45

标签: ios swift uialertcontroller

我用三个操作按钮创建了UIAlertController,现在我想在每个按钮上将文本设置为TextView。 问题是,当我单击按钮时,先前单击的按钮的结果会打印在TextView上。

  

我的代码

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)

    }

}

1 个答案:

答案 0 :(得分:2)

我认为可能的问题是,您正在模拟器中对此进行测试,并且计算机的图形配置较低,当我在旧的Mac mini中测试代码时,也会发生同样的事情。您可以做两件事:-< / p>

  1. 在物理设备中测试代码
  2. 尝试最小化模拟器中的应用程序,然后返回该应用程序,您将看到效果,或者来回更改视图以查看效果。

让我知道我是否正确。

相关问题