Xcode-如何循环UIAlert(带有文本字段),直到输入正确的值? [英] Xcode - how to loop a UIAlert (with text field) until a correct value is entered?

查看:54
本文介绍了Xcode-如何循环UIAlert(带有文本字段),直到输入正确的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在使用iOS应用程序中的Xcode ...

I'm currently working in Xcode on an iOS app...

我已经设置了一个UIAlertView(带有一个问题作为消息),并弹出一个文本字段来检索响应.

I've set up a UIAlertView (with a question as the message ) to pop up with a text field to retrieve a response.

所需的功能是,在文本字段中输入错误的值时,UIAlert将循环...直到输入正确的响应.此时,UIAlert将被撤消.

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.

我已经在Google上进行了广泛的搜索,无法提出解决方案...任何回复将不胜感激!

I've done extensive google searching and can't come up with a solution... Any responses would be much appreciated!

推荐答案

将您的类设置为UITextFieldDelegate,并假定您具有UIAlertView的名为alertView的属性:

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];
    }
}

如果您想要更细粒度的匹配,请在寻找子字符串时使用NSRegularExpression或NSRange:

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
}

这篇关于Xcode-如何循环UIAlert(带有文本字段),直到输入正确的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆