Reputation: 1266
I'm trying to put two text fields in one row inside a UIAlertController (formerly UIAlertView) in order to create a phone number inputs - one for the country code and one for the number (I need them separated). Is it possible to do this? Answers in swift will be appreciated. :)
Upvotes: 2
Views: 2045
Reputation: 1266
So this is my piece of Swift code. It works for me. I hope it will work for somebody else too.
class NumbersViewController: UIViewController, UITextFieldDelegate {
var codeTextField: UITextField?;
var numberTextField: UITextField?;
override func viewDidLoad() {
super.viewDidLoad();
}
override func viewDidAppear(animated: Bool) {
self.askForNumber();
}
func askForNumber(prefix: String = ""){
var title = "";
var message = "Enter your mobile number\n\n\n";
var alert = UIAlertController(title: title, message: message, preferredStyle: UIAlertControllerStyle.Alert);
alert.modalInPopover = true;
func handleOk(alertView: UIAlertAction!){
var code: String = self.codeTextField!.text;
var number: String = self.numberTextField!.text;
}
var inputFrame = CGRectMake(0, 70, 270, 25);
var inputView: UIView = UIView(frame: inputFrame);
var prefixFrame = CGRectMake(7, 5, 10, 10);
var prefixLabel: UILabel = UILabel(frame: prefixFrame);
prefixLabel.text = "+";
var codeFrame = CGRectMake(20, 0, 65, 25);
var countryCodeTextField: UITextField = UITextField(frame: codeFrame);
countryCodeTextField.placeholder = "Code";
countryCodeTextField.borderStyle = UITextBorderStyle.RoundedRect;
countryCodeTextField.keyboardType = UIKeyboardType.DecimalPad;
var numberFrame = CGRectMake(90, 0, 170, 25);
var myNumberTextField: UITextField = UITextField(frame: numberFrame);
myNumberTextField.placeholder = "Number - digits only";
myNumberTextField.borderStyle = UITextBorderStyle.RoundedRect;
myNumberTextField.keyboardType = UIKeyboardType.DecimalPad;
self.codeTextField = countryCodeTextField;
self.numberTextField = myNumberTextField;
inputView.addSubview(prefixLabel);
inputView.addSubview(self.codeTextField!);
inputView.addSubview(self.numberTextField!);
alert.view.addSubview(inputView);
alert.addAction(UIAlertAction(title: "Ok", style: UIAlertActionStyle.Default, handler:handleOk));
self.presentViewController(alert, animated: true, completion: nil);
} }
Upvotes: 4
Reputation: 582
I think that Apple discourage this approach, but anyway AlertView have addSubview method, so you can create your textfields and add them to the AlertView. Remember to get the TextField become first responder.
But again, Apple discourage this approach.
Upvotes: 0