如何将文本字段添加到警报视图? Xcode 4.5 iOS6
Posted
技术标签:
【中文标题】如何将文本字段添加到警报视图? Xcode 4.5 iOS6【英文标题】:How to add textfield to an alert view? Xcode 4.5 iOS6 【发布时间】:2013-01-31 02:26:05 【问题描述】:如何将文本字段添加到警报视图?我正在尝试做一个应用程序,在应用程序提交编辑之前,用户必须通过在所述警报视图上输入他/她的密码来进行第一次身份验证,但我该怎么做呢?我搜索的代码似乎不再起作用了。这里是:
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Log In" message:@"Please enter your Password" delegate:self cancelButtonTitle:@"Log In" otherButtonTitles:@"Cancel", nil];
[alert addTextFieldWithValue:@"" label:@"Password"];
UITextField *tf = [alert textFieldAtIndex:0];
tf.clearButtonMode = UITextFieldViewModeWhileEditing;
tf.keyboardType = UIKeyboardTypeAlphabet;
tf.keyboardAppearance = UIKeyboardAppearanceAlert;
tf.autocapitalizationType = UITextAutocapitalizationTypeWords;
tf.autocapitalizationType = UITextAutocorrectionTypeNo;
错误说
"No visible @interface for 'UIAlertView'
declares the selector 'addTextFieldWithValue:label:'"
on the [alert addTextFieldWithValue:@"" label:@"Password"];
我也想问一下如何在警报视图的确认按钮上输入代码。
【问题讨论】:
【参考方案1】:alert.alertViewStyle = UIAlertViewStylePlainTextInput;
或者如果您需要它是安全的(用于密码)
alert.alertViewStyle = UIAlertViewStyleSecureTextInput;
编辑:
我不是 100% 确定您所说的“将代码放在确认按钮上”是什么意思,但如果您想知道他们是按下确认还是取消,您只需要实施
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
//check the button index and do something if it's the right one.
【讨论】:
没有这样的事情 :) 我们所有人在某些时候都是新手。祝你的应用好运!【参考方案2】:将文本字段添加到警报视图 (Swift):
let pincodeAlert:UIAlertController = UIAlertController(title: "Hello", message: "Enter a new passcode", preferredStyle: UIAlertControllerStyle.Alert)
pincodeAlert.addTextFieldWithConfigurationHandler( (pinCodeTextField:UITextField!) -> Void in
pinCodeTextField.placeholder = "password"
pinCodeTextField.secureTextEntry = true
)
//Add the textField
pincodeAlert.addTextFieldWithConfigurationHandler( (pinCodeTextField2:UITextField!) -> Void in
pinCodeTextField2.placeholder = "Confirm password"
pinCodeTextField2.secureTextEntry = true
)
pincodeAlert.addAction(UIAlertAction(title: "OK", style: UIAlertActionStyle.Default, handler: (action) -> Void in
//check entered passcodes
))
pincodeAlert.addAction(UIAlertAction(title: "Cancel", style: UIAlertActionStyle.Cancel, handler: nil))
presentViewController(pincodeAlert, animated: true, completion: nil)
要检查复选框中的数据,只需将其添加到“确定”操作处理程序中即可:
let passcodeEntered:String = (pincodeAlert.textFields?.first as UITextField).text
【讨论】:
以上是关于如何将文本字段添加到警报视图? Xcode 4.5 iOS6的主要内容,如果未能解决你的问题,请参考以下文章
在表格视图控制器中添加容器视图时,如何将其移动到最底部? (迅速)