tags:

views:

472

answers:

1

I have five fields for number input, and each of these fields must be in a certain number range. For example, Field One should be above 40 and below 100. Field Two should be above 1.25 and below 5.5. You get the idea for the rest.

I am able to make an UIAlertView, but only for the button that is pressed to calculate each of the fields. If the end-user inputs wrong data for each of the five fields, they will then see five different Alerts and isn't really UI friendly.

I would like to have it where the user inputs the number, and when the user goes to the next field, to evaluate their input and then send an Alert View. This way the user would automatically know before they input another field. I think this would work much easier for the UI. But, I don't know the best way to do this and I'm just learning iPhone SDK and ObjC.

Here is my code I have used for my buttonPressed button, which as you can see takes each of the five (four in this case) fields, assigns them to a variable, creates two test UIAlerts, and does some math before outputting the answer. Keep in mind I'm a n00b here.....

//calculate total of variables when user clicks button
-(IBAction) submitYourName;
{
    /*********************************************************
     declare our variables to strings
     ********************************************************/
    NSString *userNameOne = txtUserName.text;
    float numOne = [userNameOne intValue];
    NSString *userNameTwo = txtUserName2.text;
    float numTwo = [userNameTwo intValue];
    NSString *userNameThree = txtUserName3.text;
    float numThree = [userNameThree intValue];
    NSString *userNameFour = txtUserName4.text;
    float numFour = [userNameFour intValue];

    /*********************************************************
     allow error handling for our numbers
     ********************************************************/
    if(numOne < 40 || numOne > 100)
    {
     UIAlertView *alert = [[UIAlertView alloc]
            initWithTitle:@"User Error"
            message:@"Your age must be at least 40 years old and less than 100 years old"
            delegate:nil
            cancelButtonTitle:@"OK"
            otherButtonTitles:nil];
     [alert show];
     [alert release];
    }

    if(numTwo < 40 || numTwo > 100)
    {
     UIAlertView *alert = [[UIAlertView alloc]
            initWithTitle:@"User Error"
            message:@"Your age must be at least 40 years old and less than 100 years old"
            delegate:nil
            cancelButtonTitle:@"OK"
            otherButtonTitles:nil];
     [alert show];
     [alert release];
    }


    /*********************************************************
     calculate our mathematics
     ********************************************************/
    float one = 3;
    float main = numOne * one;


    /*********************************************************
     produce answer
     ********************************************************/
    lblUserTypedName.text = [[NSString alloc] initWithFormat: @"you input %2.2f %2.2f %2.2f %2.2f", main, numTwo, numThree, numFour];
}
A: 

I'd say invoke the UITextFieldDelegate's :

- (void)textFieldDidEndEditing:(UITextField *)textField;

method, and in it check for the input validation.

BTW, why would you display sooo many alerts? I'd suggest creating an NSString that would go through each if and add something like "Field 1's input is not valid." and so on, and in the end display one UIAlertView with all of the details. That would be user friendly and IMO better than your current solution.

That's just off the top of my mind, but I hope this would be of some help to you. ~ Natanavra.

natanavra
i am confused. do i use the UITextFieldDelegate to create the input handling or creating a NSString?
HollerTrain
The UITextFieldDelegate method will be invoked when the user moves from textField to another, allowing you to check each field individually after input.The NSString creation is stacking your message and afterwards displaying everything in one Alert.
natanavra
do i connect the textFieldDidEndEditing to every field? Then it will check each field?
HollerTrain
Yes. The method will be called on each textField after you finish editing each and every one.
natanavra
awesome, that worked perfectly. do you have any advice or tutorials or something visual i can see to learn how to do this "NSString that would go through each if and add something like "Field 1's input is not valid." and so on". I am now able to do the if/else for each one but having one single Alert would be much better option :)
HollerTrain
No tuts here... You could use something like:NSString *message;if(...)message = [NSString stringWithFormat:@"%@ <Error Here>", message];And so on...
natanavra
would it be possible for me to get your email, as I have more questions and not finding much help online ;)
HollerTrain
Hmm... Not feeling to comfortable posting it here... I'll write a junk mail here, but I'll check it:junk4register at gmail.com
natanavra