问题
I'm currently working in Xcode on an iOS app...
I've set up a UIAlertView (with a question as the message ) to pop up with a text field to retrieve a response.
The desired functionality is that upon entering an incorrect value into the text field, the UIAlert would loop... Until the correct response is entered. At this point, the UIAlert would be dismissed.
I've done extensive google searching and can't come up with a solution... Any responses would be much appreciated!
回答1:
Set your class as the UITextFieldDelegate, and assuming you have a property for the UIAlertView called alertView:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
NSString *textFieldString = textField.text;
if ([textFieldString isEqualToString:@"something"])
{
[self.alertView dismissWithClickedButtonIndex:-1 animated:YES];
}
}
If you want more finely grained matching, use NSRegularExpression or NSRange if you're looking for a substring:
NSRange result = [textFieldString rangeOfString:searchText options:(NSCaseInsensitiveSearch|NSDiacriticInsensitiveSearch)];
if (result.location != NSNotFound)
{
// dismiss
}
来源:https://stackoverflow.com/questions/9406138/xcode-how-to-loop-a-uialert-with-text-field-until-a-correct-value-is-entered