2015-05-07 10 views
6

In meiner Anwendung möchte ich eine Warnung mit einem Textfeld. Nachdem ich auf "Fertig" geklickt habe, möchte ich die Texteingabe in einem String speichern. Nach dem Klicken auf "Abbrechen" möchte ich nur die Warnung schließen. Ich habe meine Alarm wie folgt erstellt:Probleme beim Abrufen von Text aus UIAlertView-Textfeld

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) 

Der Alarm wie folgt aussieht:

My alert

Ich möchte wissen, wie Sie den Text aus dem Textfeld zu bekommen, und wie sich die Ereignisse schaffen für die Schaltfläche "Fertig" und die Schaltfläche "Abbrechen".

+1

Sie müssen die entsprechenden 'implementieren UIAlertViewDelegate 'Methoden (und legen Sie die' delegate' Eigenschaft der Alarmansicht fest). – rmaddy

+0

Kann mir jemand ein Beispiel mit diesem "UIAlerrViewDelegate" geben? –

+1

Bitte machen Sie eine [kleine Suche] (http://stackoverflow.com/search?q=uialertview+swift). – rmaddy

Antwort

27

Sie gehen mit UIAlertController statt UIAlertView zu implementieren.

Ich habe bereits implementiert und getestet mit UIAlertController für was Sie eigentlich wollen. Bitte versuchen Sie den folgenden Code

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

Genau das, was ich suche. –

+1

Danke dafür .. :) –

+2

@DharmeshKheni Froh, dass es dir geholfen hat! – iRiziya

2

Sie die UIAlertViewDelegate der

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

Für iOS 8+ Sie UIAlertController statt UIAlertView verwenden sollten. Zur Unterstützung von iOS 7 sollten Sie (UIAlertViewDelegate) implementieren:

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

Für 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) 

} 

Ich hoffe, es wird helfen, jemand anderes :)