Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add a TextField to UIAlertView in Swift

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

var altMessage = UIAlertController(title: "Warning", message: "This is Alert Message", preferredStyle: UIAlertControllerStyle.Alert) altMessage.addAction(UIAlertAction(title: "Done", style: UIAlertActionStyle.Default, handler: nil)) self.presentViewController(altMessage, animated: true, completion: nil) 

I have this code for textfield , how can I show this in UIAlerView

var my:UITextField = UITextField(frame: CGRectMake(0, 0, 10, 10)) 

I also tried this code:

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

When I specify the AlertStyle plainText, it shows a TextField with default placeholder, "Login".. I want to change that, I want to show a Keyboard of Decimal Pad. I also want to handle the value the user enters into the textField. Can someone help me with this?

like image 891
Apple Kid Avatar asked Jun 07 '14 04:06

Apple Kid


People also ask

What is textfield in Swift?

A TextField is a type of control that shows an editable text interface. In SwiftUI, a TextField typically requires a placeholder text which acts similar to a hint, and a State variable that will accept the input from the user (which is usually a Text value).

What is textfield in iOS?

A control that displays an editable text interface. iOS 13.0+ iPadOS 13.0+ macOS 10.15+ Mac Catalyst 13.0+ tvOS 13.0+ watchOS 6.0+


1 Answers

You can access the textfield with:

let textField = alert.textFieldAtIndex(0) 

Then to change the placeholder text:

textField.placeholder = "Foo!" 

And the keyboard type:

textField.keyboardType = ... 
like image 152
David Berry Avatar answered Oct 16 '22 16:10

David Berry