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 08-26-2011, 10:53 PM   #1 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: Plymouth, UK
Age: 22
Posts: 4
Wondering Soul is on a distinguished road
Question Resigning UITextField not working

I know you see this question posted a lot, but I've yet to find an answer that explain the entire process of how this needs to be done. I've written the following code and it just doesn't work, at all.

I have a UIScrollView that gets populated with a few labels and text fields programatically. The input is numeric only so there is no "Done" button on the keyboard to resign things that way. I need it to be done when the user touches anywhere outside of the UITextField that just had focus. So I setup a "backgroundTouched" method that is called on the Touch Down event on my Scroll View. Here is what I have:

-(void)viewDidLoad Method
Code:
for (int i = 0; i < 14; i++)
        {
            int ii = i + 1;

            textFieldCollection = [[UITextField alloc] initWithFrame:CGRectMake(193, textFieldOffset, 64, 31)];
            textFieldCollection.placeholder = @"score";
            textFieldCollection.backgroundColor = [UIColor whiteColor];
            textFieldCollection.borderStyle = UITextBorderStyleBezel;
            textFieldCollection.textAlignment = UITextAlignmentCenter;
            textFieldCollection.keyboardType = UIKeyboardTypeNumberPad;
            textFieldCollection.tag = ii;
            [textFieldCollection addTarget:self action:@selector(backgroundTouched:) forControlEvents:UIControlEventEditingDidEnd];
            textFieldOffset += 38;

            [self.scrollView addSubview:textFieldCollection];
            [textFieldCollection release];
        }
-(IBAction)backgroundTouched Method
Code:
- (IBAction)backgroundTouched:(id)sender
{
    [sender resignFirstResponder];
}
Am I missing something obvious or is this just something that is very tedious to do?

Thanks in advance!
Wondering Soul is offline   Reply With Quote
Old 08-27-2011, 01:39 AM   #2 (permalink)
Registered iOS Developer
 
Join Date: Jul 2011
Posts: 110
studentlost is on a distinguished road
Post

Quote:
Originally Posted by Wondering Soul View Post
I know you see this question posted a lot, but I've yet to find an answer that explain the entire process of how this needs to be done. I've written the following code and it just doesn't work, at all.

I have a UIScrollView that gets populated with a few labels and text fields programatically. The input is numeric only so there is no "Done" button on the keyboard to resign things that way. I need it to be done when the user touches anywhere outside of the UITextField that just had focus. So I setup a "backgroundTouched" method that is called on the Touch Down event on my Scroll View. Here is what I have:

-(void)viewDidLoad Method
Code:
for (int i = 0; i < 14; i++)
        {
            int ii = i + 1;

            textFieldCollection = [[UITextField alloc] initWithFrame:CGRectMake(193, textFieldOffset, 64, 31)];
            textFieldCollection.placeholder = @"score";
            textFieldCollection.backgroundColor = [UIColor whiteColor];
            textFieldCollection.borderStyle = UITextBorderStyleBezel;
            textFieldCollection.textAlignment = UITextAlignmentCenter;
            textFieldCollection.keyboardType = UIKeyboardTypeNumberPad;
            textFieldCollection.tag = ii;
            [textFieldCollection addTarget:self action:@selector(backgroundTouched:) forControlEvents:UIControlEventEditingDidEnd];
            textFieldOffset += 38;

            [self.scrollView addSubview:textFieldCollection];
            [textFieldCollection release];
        }
-(IBAction)backgroundTouched Method
Code:
- (IBAction)backgroundTouched:(id)sender
{
    [sender resignFirstResponder];
}
Am I missing something obvious or is this just something that is very tedious to do?

Thanks in advance!

Hey Wondering Soul!

Here is a code snippet I have used in one of my apps to resign the keyboard whenever the user taps outside of the field.

Hope this is what you're looking for and helps!

Code:
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event 
{
    UITouch * touch = [touches anyObject];
    if(touch.phase == UITouchPhaseBegan) 
    {
        [textField1 resignFirstResponder];
        [textField2 resignFirstResponder];
        [textField3 resignFirstResponder];
        [textField4 resignFirstResponder];
    }
}
studentlost is offline   Reply With Quote
Old 08-27-2011, 07:03 PM   #3 (permalink)
Registered Member
 
Join Date: Aug 2011
Location: Plymouth, UK
Age: 22
Posts: 4
Wondering Soul is on a distinguished road
Default

Thanks for you reply.

I have tried doing it this way before but I was curious as to whether or not there was a "cleaner" way to do it. By "cleaner" I mean a way in which I can resign any UITextField on the UIScrollView purely by seeing which one was made the firstResponder, then checking the input, and resigning it then, if necessary.

I'm only asking if this way is possible because I have some data entry views that have over 35 separate UITextField's and having to resign each one individually will soon become quite a hassle. After changing some code around and doing a bit of debugging, I have been able to work out when my backgroundTouched method is being called; it is running at the correct time now but I have come across another, more serious problem.

When I try and enter some data (integer values) into the UITextFields', I get the following: EXC_BAD_ACCESS. Now, I'm finding this really difficult to debug. I can't follow the trace back through the stack very well at all. It only shows where the error occured in assembly language. I know that this error is occuring because I am trying to access an object that has been released. Am I correct in thinking that? If so, the only place it can be occuring is when I try to check the value of the UITextField in my backgroundTouched method to validate the input. Would that be a possibility?

Thanks again.
Wondering Soul is offline   Reply With Quote
Reply

Bookmarks

Tags
resign responder, text field

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: 391
16 members and 375 guests
7twenty7, chiataytuday, cristofercolmbos, dedeys78, fiftysixty, gmarro, iOS.Lover, jonathandeknudt, kilobytedump, Matrix23, raymng, ryantcb, stanny, tymex, UMAD, xerohuang
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,669
Threads: 94,121
Posts: 402,903
Top Poster: BrianSlick (7,990)
Welcome to our newest member, dedeys78
Powered by vBadvanced CMPS v3.1.0

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