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 > iPhone SDK Development - Advanced Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 03-25-2011, 12:21 PM   #1 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 4
Jack Fair_Weather is on a distinguished road
Default Change TextField focus after x # characters entered:

Suppose I have 2 TextFields (TF1 and TF2). I want to automatically change the focus from TF1 to TF2 after the user has entered 3 characters into TF1.

This can be accomplished by responding to the shouldChangeCharactersInRange: message sent to the TextField's delegate.
//-------------------------------------------------------------------------
- (BOOL)textFieldUITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementStringNSString *)string
{
if (range.location == 2)
{
TF1.text = [TF1.text stringByAppendingString:string];
[textField resignFirstResponder];
[TF2 becomeFirstResponder];
}
}
---------------------------------------------------------------------------
Say, for example, that the last character entered in FT1 is a "6".
The "6" is sent to this method in the string parameter and the fact that it is the 3rd character is recognized by range.location == 2. At this point I append the "6" to the first 2 characters. This works fine except for the fact that that 3rd character ("6") is loaded into TF2 along with the focus. I've worked through all of the TextField "Shoulds and Dids", but haven't been able prevent the last character being loaded into TF2. Nor have I been able to automatically delete it:

????? Thanks. Jack
Jack Fair_Weather is offline   Reply With Quote
Old 03-25-2011, 03:58 PM   #2 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 4
Jack Fair_Weather is on a distinguished road
Default

Quote:
Originally Posted by Jack Fair_Weather View Post
Suppose I have 2 TextFields (TF1 and TF2). I want to automatically change the focus from TF1 to TF2 after the user has entered 3 characters into TF1.

This can be accomplished by responding to the shouldChangeCharactersInRange: message sent to the TextField's delegate.
//-------------------------------------------------------------------------
- (BOOL)textFieldUITextField *)textField shouldChangeCharactersInRange:
(NSRange)range replacementStringNSString *)string
{
if (range.location == 2)
{
TF1.text = [TF1.text stringByAppendingString:string];
[textField resignFirstResponder];
[TF2 becomeFirstResponder];

return NO;
}
}
---------------------------------------------------------------------------
Say, for example, that the last character entered in FT1 is a "6".
The "6" is sent to this method in the string parameter and the fact that it is the 3rd character is recognized by range.location == 2. At this point I append the "6" to the first 2 characters. This works fine except for the fact that that 3rd character ("6") is loaded into TF2 along with the focus. I've worked through all of the TextField "Shoulds and Dids", but haven't been able prevent the last character being loaded into TF2. Nor have I been able to automatically delete it:

????? Thanks. Jack
OOOOPs....Easy to do. Just return NO
Jack Fair_Weather is offline   Reply With Quote
Old 05-23-2012, 07:03 AM   #3 (permalink)
New User
 
Join Date: May 2012
Posts: 1
djdeens is an unknown quantity at this point
Send a message via Skype™ to djdeens
Default

Code:
Code:
 - (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{        
    NSString *strPassword = [textField.text stringByReplacingCharactersInRange:range withString:string];
    if( [strPassword length] > 0 ){ 
        textField.text = string;        
        UIResponder* nextResponder = [textField.superview viewWithTag:(textField.tag + 1)];
        if (nextResponder) {
            [nextResponder becomeFirstResponder];
        }        
        return NO;
    }    
    return YES;
}



Regards,
Danish Kharadi
Wizsoft Technologies
http://wwww.wizsoft.com.pk
djdeens is offline   Reply With Quote
Reply

Bookmarks

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: 382
14 members and 368 guests
baja_yu, checkright, Duncan C, epaga, fvisticot, Meoz, mer10, Newbie123, Objective Zero, Punkjumper, reficul, rraagg, sacha1996, skrew88
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,642
Threads: 94,110
Posts: 402,857
Top Poster: BrianSlick (7,990)
Welcome to our newest member, pinacate
Powered by vBadvanced CMPS v3.1.0

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