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

Mockup & CodeGen, iPhone & iPad
($9.99)

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

Manu
($0.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 09-17-2009, 02:15 AM   #1 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 23
Default Limit UITestField to 9 characters

HI, I'm brand new to iPhone SDK and Objective-C and I can't work out how to limit the number of characters in a UITextField. When I searched it on the internet I found a few answers but they all said to put some code in the UITextFields delegate, but I don't have a UITextField delegate. And then I wondered how do you implement it (i.e. in Interface Builder cntrl-drag to delegate function). Thank you.
alexstuckey is offline   Reply With Quote
Old 09-17-2009, 02:24 AM   #2 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: India
Posts: 239
Thumbs up

Quote:
Originally Posted by alexstuckey View Post
HI, I'm brand new to iPhone SDK and Objective-C and I can't work out how to limit the number of characters in a UITextField. When I searched it on the internet I found a few answers but they all said to put some code in the UITextFields delegate, but I don't have a UITextField delegate. And then I wondered how do you implement it (i.e. in Interface Builder cntrl-drag to delegate function). Thank you.
you can use like this functionality
on click
if(textfield.length == 4)
{
code
}
else
{
Alert
}

Thanks you
milanjansari is offline   Reply With Quote
Old 09-17-2009, 02:37 AM   #3 (permalink)
Daddy Cool
 
MarkC's Avatar
 
Join Date: Mar 2009
Location: Yorkshire, England
Age: 100
Posts: 1,472
Default

Elaborating a bit on the above, here's a piece of code that'll limit the length for you Alex, as well as stopping you entering %, & and ". Define kmaxCharacters in the header...

Code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
	if (textField.text.length >= kmaxCharacters && range.length == 0)
	{
		return NO; // return NO to not change text
	}else{
		if ([string isEqualToString:@"\""] || [string isEqualToString:@"-"] || [string isEqualToString:@"%"] ) {
			return NO;
		}
		return YES;
	}
}
MarkC is offline   Reply With Quote
Old 09-17-2009, 04:03 PM   #4 (permalink)
Registered Member
 
Join Date: Aug 2009
Posts: 23
Default

Quote:
Originally Posted by MarkC View Post
Elaborating a bit on the above, here's a piece of code that'll limit the length for you Alex, as well as stopping you entering %, & and ". Define kmaxCharacters in the header...

Code:
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
	if (textField.text.length >= kmaxCharacters && range.length == 0)
	{
		return NO; // return NO to not change text
	}else{
		if ([string isEqualToString:@"\""] || [string isEqualToString:@"-"] || [string isEqualToString:@"%"] ) {
			return NO;
		}
		return YES;
	}
}
Thank you for that, I tried to put it in but I don't know which file it goes in and/or if I have to do any linking between the UITextField and the code.
alexstuckey is offline   Reply With Quote
Reply

Bookmarks

Tags
characters, delegate, limit, uitextfield

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
» Stats
Members: 158,884
Threads: 89,229
Posts: 380,763
Top Poster: BrianSlick (7,129)
Welcome to our newest member, karlam963
Powered by vBadvanced CMPS v3.1.0

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