Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 10-25-2010, 10:13 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 14
Andy Elliott is on a distinguished road
Default Validate user input in textfield (minimum requirement)

In my AddItemViewController I have a text field for entering data (duh)
My challenge is even if no text is input to the field I can save a "blank" item to the dictionary.
Is there a way to require input before making "save" available?
Thanks for the help.
Andy Elliott is offline   Reply With Quote
Old 10-25-2010, 10:33 AM   #2 (permalink)
Cocoa Junkie
 
Duncan C's Avatar
 
Join Date: Dec 2008
Location: Northern Virginia
Posts: 6,003
Duncan C has a spectacular aura about
Default

Quote:
Originally Posted by Andy Elliott View Post
In my AddItemViewController I have a text field for entering data (duh)
My challenge is even if no text is input to the field I can save a "blank" item to the dictionary.
Is there a way to require input before making "save" available?
Thanks for the help.
Sure.

Add a delegate of the UITextField.

Implement the UITextFieldDelegate method textField:shouldChangeCharactersInRange:replacemen tString:

This method gets called every time the user types a character or otherwise changes the text in the field.

In that routine, check to see if the text of the text field has a length of zero. IF yes, disable your save button. If no, enable the save button. (always return true unless you want to prohibit the change the user tries to make)

You might need to call yourself with performSelector:withObject:afterDelay:0 and check the text length in the method that you call, to give the system a chance to change the text of the text field after each edit. I haven't tried the approach I'm describing, but it should work.
__________________
Regards,

Duncan C
WareTo

Check out our apps in the Apple App store


Check out this password generator app that shows various techniques including using a data container singleton object to share data between objects in your project.

See this tutorial on using UIView animations and layer animations:

See this thread on generating random, non-repeating text

Check out a very cool Macintosh Kaleidoscopes app called ScopeWorks that we released to the Mac App store.
Duncan C is offline   Reply With Quote
Old 10-25-2010, 04:01 PM   #3 (permalink)
Registered Member
 
Join Date: Sep 2010
Posts: 14
Andy Elliott is on a distinguished road
Default I'm close to an answer

Quote:
Originally Posted by Duncan C View Post
Sure.

Add a delegate of the UITextField.

Implement the UITextFieldDelegate method textField:shouldChangeCharactersInRange:replacemen tString:

This method gets called every time the user types a character or otherwise changes the text in the field.

In that routine, check to see if the text of the text field has a length of zero. IF yes, disable your save button. If no, enable the save button. (always return true unless you want to prohibit the change the user tries to make)

You might need to call yourself with performSelector:withObject:afterDelay:0 and check the text length in the method that you call, to give the system a chance to change the text of the text field after each edit. I haven't tried the approach I'm describing, but it should work.
I've added the ...shouldChangeCharactersInRange method... and it works.
As an example !([newString length] > 6); limits character to six

Would you explain how to have the code NOT accept less than one or 0 prior to enabling the save button?
The second question is: how do I disable the save button to start with.
The code below is what I'm using.

-(IBAction) saveid)sender {
NSLog(@"Save pressed");
if (aTitle !=nil) {
[titleArray removeObject:aTitle];
self.aTitle = nil;
}


//create a new title dictionary for the new values
NSMutableDictionary* newTitle = [[NSMutableDictionary alloc] init];
[newTitle setValue:titleTextField.text forKey:NAME_KEY];
[newTitle setValue:titleDetailView.text forKey:TITLEINFO_KEY];

//add it to the master title array and release our reference
[titleArray addObject:newTitle];
[newTitle release];

NSSortDescriptor *nameSorter = [[NSSortDescriptor alloc] initWithKey:NAME_KEY ascending:YES selector:@selector(caseInsensitiveCompare];
[titleArray sortUsingDescriptors:[NSArray arrayWithObject:nameSorter]];
[nameSorter release];

[self dismissModalViewControllerAnimated:YES];

}
-(IBAction) cancelid)sender {
NSLog (@"Cancel pressed");
[self dismissModalViewControllerAnimated:YES];
}
Andy Elliott is offline   Reply With Quote
Old 10-25-2010, 04:11 PM   #4 (permalink)
Registered Member
 
Join Date: Jul 2010
Posts: 327
thomashw is on a distinguished road
Default

Create an IBOutlet for your save button and connect it in Interface Builder. In IB, I would disable it by default. Then in your code you can enable it once there are 6 or more characters.
thomashw is offline   Reply With Quote
Reply

Bookmarks

Tags
interface builder, iphone 4, text field save

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 327
12 members and 315 guests
7twenty7, chiataytuday, condor304, Desert Diva, Domele, dre, dreamdash3, mottdog, palme2elie, Paul Slocum, schmallegory
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,659
Threads: 94,118
Posts: 402,895
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dreamdash3
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:16 AM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0