简体   繁体   English

UIAlertView在每次输入数字后无意显示

[英]UIAlertView showing unintentionally after each number entry

Here is a problem with my textfields. 这是我的文本字段的问题。

-(void)textFieldTextDidChange:(NSNotification *)notif
{
    event.eventName = eventTextField.text;
    event.eventPlace = eventPlaceTextField.text;
    event.eventWinery = wineryTitleLabel.text;
    int vintageVal = [vintageTextField.text intValue];
    if([vintageTextField.text length]>0 && [vintageTextField.text length] == 4)
    {
        event.eventVintage = vintageVal;
    }
    else 
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"!!! MESSAGE !!!"
                 message:@" Enter the Valid year in Format YYYY"
                 delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
        [alert show]; 
        [alert release];
        return;
    }
}

If the user enters the year with four digits then only it should be saved into event.eventVitage But when entering data I'm getting the alert view every single number. 如果用户用四位数字输入年份,则只应将其保存到event.eventVitage但是,当输入数据时,我会在每个数字上看到警报视图。

What am I doing wrong? 我究竟做错了什么?

Your problem is with your if statement. 您的问题在于您的if语句。

if([vintageTextField.text length]>0 && [vintageTextField.text length] == 4) if([vintageTextField.text length]>0 && [vintageTextField.text length] == 4)

In the && part of it you are checking to see if the length is 4. If the lenth is not 4 then the else is called. 在它的&&部分中,您要检查长度是否为4。如果长度不是4,则调用else。 You need to check if the length is less than or equal to 4 <= and so the alert will only show when the length is greater than 4-------------------------------------------------------------------------------------! 您需要检查长度是否小于或等于4 <= ,因此仅当长度大于4时才会显示警报。 -------------------------------------------------- ---------------!

if([vintageTextField.text length]>0 && [vintageTextField.text length] <= 4) if([vintageTextField.text length]>0 && [vintageTextField.text length] <= 4)

Hope this helps and have fun coding 希望这会有所帮助,并祝您编程愉快

    if([vintageTextField.text length]>0 && [vintageTextField.text length] == 4)
{
    event.eventVintage = vintageVal;
}
else 
{....

This says: if your textString is 4 (the larger than 0 is redundant here..) this mean that when it is 1-2-3 it will run the else code. 这表示:如果您的textString为4(大于0在这里是多余的。。)这意味着当它为1-2-3时,它将运行else代码。

In situations like this I often either gray the text out until the user has entered enough digits or disable the "save" button. 在这种情况下,我经常将文本显示为灰色,直到用户输入足够的数字为止,或者禁用“保存”按钮。 If you don't have any way for the user to indicate that he is done typing, it is hard to foresee. 如果您无法让用户指示他已经完成输入,则很难预见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM