我试图在UIAlertController(以前称为UIAlertView)中的一行中放置两个文本字段,以便创建一个电话号码输入 - 一个用于国家代码,另一个用于数字(我需要它们分开)。是否有可能做到这一点? swift中的答案将不胜感激。 :)
答案 0 :(得分:2)
所以这是我的Swift代码。这个对我有用。我希望它也适用于其他人。
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);
} }
答案 1 :(得分:0)
我认为Apple不鼓励这种方法,但无论如何AlertView都有addSubview方法,因此您可以创建文本字段并将它们添加到AlertView中。 记得让TextField成为第一响应者。
但同样,Apple不鼓励这种做法。