2016-04-21 24 views
5

Tôi có một UIAlertController (Alert style) trong Swift và tất cả hoạt động tốt. Tuy nhiên, UITextField mà tôi đã thêm vào nó là một trường tùy chọn mà người dùng không bắt buộc phải nhập văn bản vào. Vấn đề là khi tôi hiển thị này UIAlertController, bàn phím xuất hiện đồng thời với trường văn bản được chọn theo mặc định. Tôi không muốn bàn phím xuất hiện trừ khi người dùng nhấn vào số UITextField. Điều này có thể giải quyết như thế nào?Ngăn bàn phím tự động xuất hiện với UIAlertController

let popup = UIAlertController(title: "My title", 
     message: "My message", 
     preferredStyle: .Alert) 
    popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in 
     optionalTextField.placeholder = "This is optional" 
    } 
    let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in 
     let optionalTextField = popup.textFields![0] 
     let text = optionalTextField.text 
     print(text) 
    } 
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil) 
    popup.addAction(cancelAction) 
    popup.addAction(submitAction) 
    self.presentViewController(popup, animated: true, completion: nil) 
+2

bạn phải làm becomeFirstResponder gọi ở một nơi khác. Hoặc bạn có thể gọi self.view endEditing: YES sau/trước khi bạn trình bày cảnh báo. –

Trả lời

4

này nên làm như lừa:

làm viewController của bạn phù hợp với UITextFieldDelegate

giao popup.textFields![0].delegate-self

thêm thẻ duy nhất để popup.textFields![0] (i sử dụng 999 trong ví dụ dưới đây)

thực hiện điều này

func textFieldShouldBeginEditing(textField: UITextField) -> Bool { 
    if textField.tag == 999 { 
    textField.tag = 0 
    return false 
    }else{ 
    return true 
    } 
} 

mã của bạn sẽ trông như thế này:

let popup = UIAlertController(title: "My title", 
            message: "My message", 
            preferredStyle: .Alert) 
    popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in 
     optionalTextField.placeholder = "This is optional" 
    } 
    popup.textFields![0].delegate = self 
    popup.textFields![0].tag = 999 
    let submitAction = UIAlertAction(title: "Submit", style: .Cancel) { (action) -> Void in 
     let optionalTextField = popup.textFields![0] 
     let text = optionalTextField.text 
     print(text) 
    } 
    let cancelAction = UIAlertAction(title: "Cancel", style: .Default, handler: nil) 
    popup.addAction(cancelAction) 
    popup.addAction(submitAction) 
    self.presentViewController(popup, animated: true, completion: nil) 
+1

tâm trí haha ​​tuyệt vời nghĩ như nhau eh: P –

3

Tôi nghĩ rằng đây là hành vi mặc định cho một TextField trong một cảnh báo, có thể xem xét một thiết kế thay thế để các textfield chỉ xuất hiện khi nó là cần thiết ...

Bây giờ, hãy thoát khỏi điều này!

Khi bạn thêm textField làm cho viewController của bạn, nó là đại biểu và thêm một thẻ vào nó.

ví dụ

popup.addTextFieldWithConfigurationHandler { (optionalTextField) -> Void in 
    optionalTextField.placeholder = "This is optional" 
    optionalTextField.delegate = self 
    optionalTextField.tag = -1 
} 

sau đó thực hiện textFieldShouldBeginEditing()

func textFieldShouldBeginEditing(textField: UITextField!) { 
    if textField.tag == -1 { 
     textField.tag = 0 
     return false 
    } else { 
     return true 
    } 
} 
Các vấn đề liên quan