UIAlertController解除按钮问题

时间:2016-02-12 03:06:12

标签: swift uialertcontroller

我目前遇到UIAlertController关闭按钮的问题。每当我选择完成按钮时,它应检查案例,然后如果满足大小写,则显示UIAlertController。然而,我面临的问题是当警报出现时,我选择了关闭按钮,然后我再次选择完成按钮,有两个关闭按钮,每次我选择完成时,会添加另一个关闭按钮。< / p>

let alertController = UIAlertController(title: "Alert", message: "Enter Information In All Fields", preferredStyle: UIAlertControllerStyle.Ale

 func doneButtonSelected(){
    if (doctorName.text == "" || doctorEmail.text == "" || doctorNumber.text == ""){
        print("This is empty")

        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
    }

    else{

    }

}

1 个答案:

答案 0 :(得分:1)

因为您将let alertController = UIAlertController(title: "Alert", message: "Enter Information In All Fields", preferredStyle: UIAlertControllerStyle.Alert)声明为全局和doneButtonSelected函数,所以您添加了一个Dismiss按钮,这就是为什么每次调用该函数时,它都附加一个Dismiss按钮。将alertController带入函数

@IBAction func doneButtonSelected(sender: UIButton) {
    let alertController = UIAlertController(title: "Alert", message: "Enter Information In All Fields", preferredStyle: UIAlertControllerStyle.Alert)

    if (doctorName.text == "" || doctorEmail.text == "" || doctorNumber.text == ""){
        print("This is empty")

        alertController.addAction(UIAlertAction(title: "Dismiss", style: UIAlertActionStyle.Default,handler: nil))
        self.presentViewController(alertController, animated: true, completion: nil)
    }

    else{

    }
}