An iPhone Application we recently produced had a order form on it. We needed to create a function that would run through all our UItextFields, extract their values, loop through, alert the user if a UITextField wasn’t completed, and execute a function (our ajax request) if all checks out.
What ‘s involved in this function
- alertview – a pop up that lets the user know they missed a required field
- array – a list of all the UITextField values
- int – a variable that increment everytime time a UITextField Checks out
- for loop – Loops through the Array
- if statement – Check the array count to the increment count
This function assumes you know a bit about the iphone, have declared and linked your IBOutlet UITextFields, and have linked your submit button in the Interface Builder to -(void)textFieldCheck:(id) sender; (which would be declared in your header file).
// Declare your Alert, NSArray, increment int
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"Please Fill in All Required Fields." delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
NSArray *fieldArray;
int i = 0;
// Load up our field array with the UITextField Values
fieldArray = [[NSArray arrayWithObjects:
[NSString stringWithFormat:@"%@",orderName.text],
[NSString stringWithFormat:@"%@",orderPhone.text],
[NSString stringWithFormat:@"%@",orderAddress.text],
[NSString stringWithFormat:@"%@",orderEmail.text],
[NSString stringWithFormat:@"%@",orderQty.text], nil] retain];
// loop through the array, alert if text field is empty, and break the the loop, other wise increment i
for (NSString *fieldText in fieldArray){
NSLog(fieldText); // make sure all is reading correctly in the console
if([fieldText isEqualToString:@""]){
[alert show];
break; // break out!!
}
i++;
}
// check that all the field were passed (i == array.count) if so execute
if(i == [[NSNumber numberWithInt: fieldArray.count] intValue]){
NSLog(@"Its all Good"); // some console fun
[self sendOrder]; //where you execute your request function
}
// release it all
[alert release];
[fieldArray release];
}
3 comments ↓
If you found this post useful click the share this button. Contribute below by adding a comment, no registration is required.
DCE Related Posts
Comparing strings in the iPhone SDK with UITextfield values Read Full Article...
How to have a UIlabel print out a float value using stringWithFormat:@"$%3.2f" Read Full Article...
One of my CoreData applications started acting buggy, and was spitting out a huge error dump on app close in the debug console. Read Full Article...
I am trying to call this within a IBAction. How do I call it correctly?
- (IBAction)addAction:(id)sender
{
// The add button was clicked, handle it here
NSLog(@”UIButton was clicked”);
[self textFieldCheck];
}
where do you put this exit to ensure it is executed regardless of how user crashes out of screen?
Local declaration of ‘i’ hides instance variable.
Leave a Comment