Xcode - how to loop a UIAlert (with text field) until a correct value is entered?

久未见 提交于 2019-12-25 05:12:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!