2015-04-02 241 views
1

我有一個UIAlertController,我正在檢查用戶輸入。當用戶沒有輸入文本字段時,我添加的OK Action按鈕應該給用戶一個警告並且不關閉警報視圖。如何禁用UIAlertController按動作按鈕時自動關閉

我處理警告,但警報視圖自動關閉。 我如何禁用自動關閉?

謝謝。

我的代碼:

var alert = UIAlertController(title: "change name and phone number", message: nil, preferredStyle: UIAlertControllerStyle.Alert) 
    alert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, 
     handler: { action in 
      //Add a comment to this line 

      let nameField: UITextField = alert.textFields![0] as UITextField 
      let phoneField: UITextField = alert.textFields![1] as UITextField 
      let name = nameField.text 
      let phone = phoneField.text 
      if name.length == 0 { 
       JLToast.makeText("Please enter name").show() 
      } else if phone.length == 0 { 
       JLToast.makeText("Please enter phone number").show() 
      } else { 
       self.sendSupportInfo(nameField.text, phone: phoneField.text) 
      } 
      println("name:: \(nameField.text), phone: \(phoneField.text)") 
    })) 
    alert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Default, handler: nil)) 

    alert.addTextFieldWithConfigurationHandler { (textField) -> Void in 
     textField.placeholder = "name" 
    } 
    alert.addTextFieldWithConfigurationHandler { (textField) -> Void in 
     textField.placeholder = "0544-444444" 
     textField.keyboardType = UIKeyboardType.PhonePad 
    } 
    self.presentViewController(alert, animated: true, completion: nil) 
+1

[阻止UIAlertController關閉]的可能重複(http://stackoverflow.com/questions/28919670/prevent-uialertcontroller-to-dismiss) – soulshined 2015-04-02 20:52:59

回答

0

你不能做到這一點。 其他選項是,您只能禁用UIAlertAction。相反,您可能想要爲此創建自己的自定義對話框。

的官方文件說:

子類 的UIAlertController類是打算按原樣使用,不支持子類。該類的視圖層次結構是私有的,不能修改。

0

U可以創建一個新的UI窗口,並使窗口級別高於警戒級別,然後在新的UI窗口上顯示警報。