2015-05-07 28 views
6

Trong đơn đăng ký của tôi, tôi muốn có cảnh báo với một trường văn bản. Sau khi nhấp vào "Done", tôi muốn lưu đầu vào của trường text trong một String. Sau khi nhấp vào "Hủy", tôi chỉ muốn đóng cảnh báo. Tôi đã tạo cảnh báo của tôi như thế này:Các vấn đề khi nhận văn bản từ trường văn bản UIAlertView

var alert = UIAlertView() 
    alert.title = "Enter Input" 
    alert.addButtonWithTitle("Done") 
    alert.alertViewStyle = UIAlertViewStyle.PlainTextInput 
    alert.addButtonWithTitle("Cancel") 
    alert.show() 

    let textField = alert.textFieldAtIndex(0) 
    textField!.placeholder = "Enter an Item" 
    println(textField!.text) 

Cảnh báo trông như thế này:

My alert

Tôi muốn biết làm thế nào để có được văn bản từ textfield, và làm thế nào để tạo ra các sự kiện cho nút "Đã hoàn tất" và nút "Hủy".

+1

Bạn cần phải thực hiện thích hợp 'UIAlertViewDelegate 'phương thức (và đặt thuộc tính' delegate' của khung nhìn cảnh báo). – rmaddy

+0

ai đó có thể cho tôi một ví dụ với "UIAlerrViewDelegate" này? –

+1

Vui lòng thực hiện [tìm kiếm nhỏ] (http://stackoverflow.com/search?q=uialertview+swift). – rmaddy

Trả lời

27

Bạn có thể đi với UIAlertController thay vì UIAlertView.

Tôi đã triển khai và thử nghiệm quá bằng UIAlertController cho những gì bạn thực sự muốn. Vui lòng thử mã sau đây

var tField: UITextField! 

    func configurationTextField(textField: UITextField!) 
    { 
     print("generating the TextField") 
     textField.placeholder = "Enter an item" 
     tField = textField 
    } 

    func handleCancel(alertView: UIAlertAction!) 
    { 
     print("Cancelled !!") 
    } 

    var alert = UIAlertController(title: "Enter Input", message: "", preferredStyle: .Alert) 

    alert.addTextFieldWithConfigurationHandler(configurationTextField) 
    alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler:handleCancel)) 
    alert.addAction(UIAlertAction(title: "Done", style: .Default, handler:{ (UIAlertAction) in 
     print("Done !!") 

     print("Item : \(self.tField.text)") 
    })) 
    self.presentViewController(alert, animated: true, completion: { 
     print("completion block") 
    }) 
+1

Chính xác những gì tôi đang tìm kiếm. –

+1

Cảm ơn vì điều này .. :) –

+2

@DharmeshKheni Vui mừng vì nó đã giúp bạn! – iRiziya

2

Bạn sẽ phải thực hiện của UIAlertViewDelegate

optional func alertView(_ alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) 
0

Đối với iOS 8+ bạn nên sử dụng UIAlertController thay vì UIAlertView. Để hỗ trợ iOS 7, bạn nên thực hiện (UIAlertViewDelegate):

func alertView(alertView: UIAlertView, clickedButtonAtIndex buttonIndex: Int) 
{ 
    //... 
    let textField = alertView.textFieldAtIndex(0) 
} 
1

Đối SWIFT 3

@IBAction func ForgotPassword(_ sender: Any) { 

    let alertController = UIAlertController(title: "Email?", message: "Please input your email:", preferredStyle: .alert) 

    let confirmAction = UIAlertAction(title: "Confirm", style: .default) { (_) in 
     if let field = alertController.textFields![0] as? UITextField { 

      // store and use entered data 

     } else { 

      print("please enter email id") 
     } 
    } 

    let cancelAction = UIAlertAction(title: "Cancel", style: .cancel) { (_) in } 

    alertController.addTextField { (textField) in 
     textField.placeholder = "Email" 
    } 

    alertController.addAction(confirmAction) 
    alertController.addAction(cancelAction) 

    self.present(alertController, animated: true, completion: nil) 

} 

Tôi hy vọng nó sẽ giúp người khác :)

Các vấn đề liên quan