views:

126

answers:

2

Hi guys,

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

here if the user enters the year with four digits then only it shoulb be saved into event.eventVitage. But here when in entering every single number im getting the alerview. How can i get out of this? guys help me to solve this.

MONISH KUMAR

A: 

Your problem is with your if statement.

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. 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-------------------------------------------------------------------------------------!

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

Hope this helps and have fun coding

Jaba
Thanks Jaba its really working fine.
monish
Hey Jaba I got nother problem here if I do so when im entering the text into other textfields the alert is coming for every single letter we entered.
monish
A: 
    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.

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.

RickiG
hey ricki thanks for ur Idea.
monish