XCODE - 如何:循环中的 UIAlertView(带有文本字段),直到将正确的值输入到字段中?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9407338/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:24:59  来源:igfitidea点击:

XCODE - How to: UIAlertView (with a text field) on a loop until correct value entered into field?

iphoneobjective-cxcodeloopsuialertview

提问by Alec McLeod

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

我目前正在 Xcode 上的 iOS 应用程序中工作...

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

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

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.

所需的功能是在文本字段中输入不正确的值时,UIAlert 将循环......直到输入正确的响应。此时,UIAlert 将被解除。

Heres what I have so far...

这是我到目前为止所拥有的......

- (void)application:(UIApplication *)application 
didReceiveLocalNotification:(UILocalNotification *)notification {

    NSString* correctAnswer = @"2";


    UIAlertView *alert = [[UIAlertView alloc] 
                          initWithTitle:@"Alarm" 
                          message:@"1 + 1 ="  
                          delegate:self 
                          cancelButtonTitle: nil 
                          otherButtonTitles:@"Continue", nil ];

    alert.alertViewStyle = UIAlertViewStylePlainTextInput;
    UITextField* answerField = [alert textFieldAtIndex:0];
    answerField.keyboardType = UIKeyboardTypeNumberPad;
    answerField.placeholder = @"answer";

    [alert show];


    // I feel like this would work, but I know it doesn't...

    NSString *answerFieldString = answerField.text;
    if ([answerFieldString isEqualToString: correctAnswer ])
    {
        [alert dismissWithClickedButtonIndex:-1 animated:YES];
    }


}

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

我已经进行了广泛的谷歌搜索,但无法提出解决方案......任何答复将不胜感激!

采纳答案by Deepesh

try this...

尝试这个...

- (BOOL)alertViewShouldEnableFirstOtherButton:(UIAlertView *)alertView
{
     NSString *answerFieldString = answerField.text;
    if ([answerFieldString isEqualToString: correctAnswer ])
    {
        [alertView dismissWithClickedButtonIndex:-1 animated:YES];
    }

}