2016-02-12 74 views
0

我目前遇到UIAlertController關閉按鈕的問題。每次我選擇完成按鈕它應該檢查大小寫,然後如果大小寫符合,顯示UIAlertController。然而,我面臨的問題是當警報出現時,我選擇解除按鈕,然後再次選擇完成按鈕,有兩個解除按鈕,每次我選擇完成時,另一個解除按鈕被添加。UIAlertController關閉按鈕問題

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

它,因爲你宣佈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{ 

    } 
} 
+0

哈哈這種低級錯誤裏面。謝謝。 – OriginalAlchemist