2015-05-07 14 views
6

Uygulamamda bir textfield ile uyarı almak istiyorum. "Bitti" yi tıkladıktan sonra textfield girişini bir String içine kaydetmek istiyorum. "İptal" i tıkladıktan sonra sadece uyarıyı kapatmak istiyorum. Bu gibi benim uyarısı oluşturduk:UIAlertView textfield'dan metin alma ile ilgili sorunlar

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) 

uyarı şuna benzer: Ben textfield metin nasıl ve olayları nasıl oluşturulacağını bilmek istiyorum

My alert

"Bitti" ve "İptal" butonu.

+1

yardımcı olacağını umuyoruz 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) } 

SWIFT için UIAlertViewDelegate yöntemleri (ve uyarı görünümünün 'temsilci' özelliğini ayarlayın). – rmaddy

+0

Birisi bana bu "UIAlerrViewDelegate" ile bir örnek verebilir? –

+1

Lütfen bir [küçük arama] yapın (http://stackoverflow.com/search?q=uialertview+swift). – rmaddy

cevap

27

Sen UIAlertController yerine UIAlertView ile gidebilir uygulamak zorunda kalacaktır.

UIAlertController'ı gerçekten istedikleriniz için kullanarak çoktan uyguladım ve test ettim. Eğer UIAlertController yerine UIAlertView kullanmalıdır 8+ iOS için aşağıdaki kodu

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

Tam olarak ne arıyorum. –

+1

Bunun için teşekkürler .. :) –

+2

@DharmeshKheni Size yardımcı olduğuna sevindim! – iRiziya

2

Sen UIAlertViewDelegate en

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

deneyin. Eğer uygulamalıdır iOS 7 (UIAlertViewDelegate) desteklemek için:

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

Ben Sen `uygun uygulamak gerekir başkası :)