【问题标题】:Xcode - how to loop a UIAlert (with text field) until a correct value is entered?Xcode - 如何循环 UIAlert(带文本字段)直到输入正确的值?
【发布时间】:2012-02-23 02:05:54
【问题描述】:
我目前正在使用 Xcode 开发 iOS 应用程序...
我设置了一个 UIAlertView(带有一个问题作为消息)弹出一个文本字段来检索响应。
所需的功能是在文本字段中输入不正确的值时,UIAlert 会循环...直到输入正确的响应。此时,UIAlert 将被解除。
我已经进行了广泛的谷歌搜索,但无法提出解决方案...任何回复将不胜感激!
【问题讨论】:
标签:
iphone
objective-c
ios
xcode
uialertview
【解决方案1】:
将您的类设置为 UITextFieldDelegate,并假设您有一个名为 alertView 的 UIAlertView 属性:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *textFieldString = textField.text;
if ([textFieldString isEqualToString:@"something"])
{
[self.alertView dismissWithClickedButtonIndex:-1 animated:YES];
}
}
如果您想要更细粒度的匹配,如果您正在寻找子字符串,请使用 NSRegularExpression 或 NSRange:
NSRange result = [textFieldString rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (result.location != NSNotFound)
{
// dismiss
}