How to add a TextField to UIAlertView in Swift

前端 未结 10 1076
耶瑟儿~
耶瑟儿~ 2020-12-23 13:41

I have this code, but I dont know how to show a textfield inside the UIAlertView.

var altMessage = UIAlertController(title: \"Warning\", message: \"This is A         


        
10条回答
  •  暖寄归人
    2020-12-23 14:24

    Try This Code (with swift):

    func configurationTextField(textField: UITextField!)
        {
            println("configurat hire the TextField")
    
            if let tField = textField {
    
                self.textField = textField!        //Save reference to the UITextField
                self.textField.text = "Hello world"
            }
        }
    
    
     func handleCancel(alertView: UIAlertAction!)
            {
               println("User click Cancel button") 
               println(self.textField.text)
            }
    
     var alert = UIAlertController(title: "Alert Title", message: "Alert Message", preferredStyle: UIAlertControllerStyle.Alert)
    
        alert.addTextFieldWithConfigurationHandler(configurationTextField)
    
        alert.addAction(UIAlertAction(title: "Close", style: UIAlertActionStyle.Cancel, handler:handleCancel))
        alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:{ (UIAlertAction)in
                println("User click Ok button")
                println(self.textField.text)
            }))
        self.presentViewController(alert, animated: true, completion: {
                println("completion block")
            })
    

    Can you see also my answer here

提交回复
热议问题