Wednesday, September 14, 2011

iOS: Dialog box, validation, numeric keyboard

Welcome to my blog! If you've stumbled across this in your travels on the internet, please take a moment click on an ad - or two! You might see something you like, *and* it helps finance the blogosphere. Think of the children!

Anyway, today, we're going to figure what the mechanics are of checking for valid values in a UITextField. These values will vary depending on the situation, so it will have to be some of your basic program validation and highlighting logic. So, actually the programming logic should be pretty straightforward - yesterday, we found some event listeners for UIText field. I'm a bit concerned about setting the delegate as self, though. When I got rid of that, I was able to make setting keyboard to numeric to work. Anyway, let's see if we can find an example.

Actually, I'm already getting the input in these two methods:

-(IBAction)userDoneEnteringStartNum:(id)sender
{
UITextField *theField = (UITextField*)sender;

// do whatever you want with this text field

NSLog(@"Start Nume is: %@", [theField text] );


}


-(IBAction)userDoneEnteringEndNum:(id)sender
{
UITextField *theField = (UITextField*)sender;

// do whatever you want with this text field

NSLog(@"End Nume is: %@", [theField text] );

}




And I also have a save button I'm planning to use. So, the question is how do I handle the validation? Well, I've actually done this before, in the Android version. Let's see what it did.

Ok, it looks like it saved a lot of the validation logic for the saved button. So, let's create a method for that.

- (IBAction) validateEntries{
NSLog(@"called validateEntries");
}


Ok, it's entered and hooked up. For some reason, I always get a kick out of hooking widgets up to code using the UIBuilder. It's not like it hasn't been done a million times before!

Ok - what's next for this crazy method? In the Android code, the first thing it does is validate the level being from 1 to 5. Well, this isn't an issue since I've set up the UIPicker control for that. Let's continue. From the Android code


if (quizStartNum < 1) {

// invalid
valid = false;

// pop up ERROR message
Toast.makeText(getBaseContext(),
"Quiz start number must be greater than zero",
Toast.LENGTH_LONG).show();

}


I wonder if it wouldn't be better to set up a separate listener for the simple validations like that? Well, anyway, let's stick with this approach for now. So, we should be saving the start an end numbers in an instance variable in the listener method, and we can do a simple check like for less than zero in there, come to think of it.

We can get the integer value like this:

NSInteger startNum = [theField.text integerValue];


Ok, so, now, how to we notify the user of the error message?

SO has a good post on this:

UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"No network connection"
message:@"You must be connected to the internet to use this app."
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alert show];
[alert release];


Ok, that worked like a charm - here's how it looks:




However - darned if we haven't lost our precious default numeric entry keyboard. Why? This is harsh - I've already spent time on this, and it was working before.

Ok, what I did was disconnect and reconnect them - and all of a sudden it started working. I'm glad that's solved.

Actually, it doesn't make sense to have validation in a method for each field, since the user could just skip down to the save button anyway. So, we'll move that display to the save button method.

Great. We're making progress, and we've learned a little debugging trick in IB. Btw, I *love* the debugger in iOS. Just click on a line to set the breakpoint and run the code. You don't have that nutty debug view that's in Eclipse. And (so far) there isn't that horrendous dead end you run into in Android when you drop into the native code by accident, but which happens all the time. I know there must be some easy way out of it short of downloading the source code, but I never figured it out. We'll resume our edit on the next post.

No comments:

Post a Comment