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)
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. –