2015-05-07 11 views
6

Nella mia applicazione voglio un avviso con un campo di testo. Dopo aver fatto clic su "Fatto", voglio salvare l'input del campo di testo in una stringa. Dopo aver fatto clic su "Annulla", desidero solo chiudere l'avviso. Ho creato il mio avviso in questo modo:Problemi con il recupero di testo dal campo di testo 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) 

L'avviso è simile al seguente:

My alert

vorrei sapere come ottenere il testo dal campo di testo, e come creare eventi per il pulsante "Fatto" e il pulsante "Annulla".

+1

È necessario implementare l'appropriato 'UIAlertViewDelegate 'metodi (e imposta la proprietà' delegate' della vista alert). – rmaddy

+0

qualcuno può darmi un esempio con questo "UIAlerrViewDelegate"? –

+1

Si prega di fare una [piccola ricerca] (http://stackoverflow.com/search?q=uialertview+swift). – rmaddy

risposta

27

Si può andare con UIAlertController invece di UIAlertView.

Ho già implementato e testato anche utilizzando UIAlertController per quello che effettivamente vuoi. Si prega di provare il seguente codice

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

Esattamente quello che sto cercando. –

+1

Grazie per questo .. :) –

+2

@DharmeshKheni Contento che ti ha aiutato! – iRiziya

2

Si dovrà implementare l'UIAlertViewDelegate

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

Per iOS 8+ si dovrebbe usare UIAlertController invece di UIAlertView. Per supportare iOS 7 è necessario implementare (UIAlertViewDelegate):

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

Per 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) 

} 

spero che aiutare qualcun altro :)

Problemi correlati