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 01-15-2009, 01:35 PM   #1 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 91
pcmofo is on a distinguished road
Default resignFirstResponder in a custom cell?

I have a username and password field in a table view that are each custom cells. It is structured like this....

UIView
->tableView
->->passwordCell
->->->passwordTextField

I am having the user input a numeric password. I have the keyboard set to the number pad. I need to resign the keyboard when the user touches anywhere outside of the text field. Here is a piece of code that I am currently using.... When placed inside the custom passwordCell.m you can press anywhere inside the cell except on the text field and it will close the keyboard.

Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Dismiss the keyboard when the view outside the text field is touched.
	[password resignFirstResponder];
}
The problem is that I need to tell it to do this in the view which contains the table which contains this custom cell so that pressing anywhere in the cell does this. I cant figure out how to access the password text field inside of a custom cell from inside a function like touchesBegin. I was thinking something like self.tableView.cell.password but that is clearly wrong. Can anyone think of a way to access this field so I can resign the keyboard? Can I create a method inside of the cell to call to close it?
pcmofo is offline   Reply With Quote
Old 01-15-2009, 03:43 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 91
pcmofo is on a distinguished road
Default

I have tried quite a few things.... I still am unable to access this....
pcmofo is offline   Reply With Quote
Old 01-15-2009, 08:54 PM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Posts: 10
tuannd is on a distinguished road
Default

If you have many text fields, each cell has one text field:
=> set the tag for the text field (e.g: passTextField.tag = 1
=> in place to want to access the text fields (in this case is touchesBegan method), add this code:

NSArray *cells = [self.tableView visibleCells];
for(UITableViewCell *cell in cells) {
UITextField *passTextField = [cell viewWithTag:1];
//do anything you want with the text field
[passTextField resignFirstResponder];
}

If you only have one text field, you can add an if() inside the for() loop to check if the cell is on the right row.

Last edited by tuannd; 01-15-2009 at 09:00 PM.
tuannd is offline   Reply With Quote
Old 01-16-2009, 04:55 AM   #4 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 3
pbcaus is on a distinguished road
Default Re: resignFirstResponder in a custom cell?

My trick for handling this situation is to set the UITextField delegate property to point to the custom cell, then use the following code to get rid of the keyboard.

Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
}
pbcaus is offline   Reply With Quote
Old 01-16-2009, 09:40 AM   #5 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 91
pcmofo is on a distinguished road
Default

Quote:
Originally Posted by pbcaus View Post
My trick for handling this situation is to set the UITextField delegate property to point to the custom cell, then use the following code to get rid of the keyboard.

Code:
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
	[textField resignFirstResponder];
	return YES;
}
Let me give you a bit more info on my code so that you can get an idea of what is going on...

this is on my settings page. GeneralSettings.m it is a UIViewController

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	// Create a table view
	UITableView *tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,0,320,460) style:UITableViewStyleGrouped]; 
	
	// Set the autoresizing mask so that the table will always fill the view
	tableView.autoresizingMask = (UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight);
	[tableView setBackgroundColor:[UIColor colorWithRed:.761 green:.878 blue:.859 alpha:1]]; // Set the background color to a custom color
	
	tableView.delegate = self;			// set the tableview delegate to this object
	tableView.dataSource = self;		// Set the table view datasource to the data source
	
	[self.view addSubview:tableView];	// Add the table to the current view
	[tableView release];
}
after that is setup I setup the cells like so...

Code:
PasswordTableCell* cell = (PasswordTableCell *)[tableView dequeueReusableCellWithIdentifier:@"SettingsViewController"];
				if (cell == nil) {
					CGRect rect;
					rect = CGRectMake(0.0, 0.0, 320.0, 60.0);
					cell = [[[PasswordTableCell alloc] initWithFrame:rect reuseIdentifier:@"PasswordTableCell"] autorelease];
					cell.title.text = @"Password:";
					cell.password.delegate = self;
					cell.password.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"userPass"];
					return cell; 
				}
so I am setting GeneralSettings to be the delegate? or am I setting the cell to be the delegate?

Here is what the cell looks like....

Code:
@implementation PasswordTableCell

@synthesize title, password;

- (id)initWithFrame:(CGRect)frame reuseIdentifier:(NSString *)reuseIdentifier {
    if (self = [super initWithFrame:frame reuseIdentifier:reuseIdentifier]) {
        // Initialization code
		CGRect rect;
		rect = CGRectMake(10.0, 5.0, 150.0, 30.0);
		title = [[UILabel alloc] initWithFrame:rect];
		[[self contentView] addSubview:title];
		
		rect = CGRectMake(110.0, 10.0, 170.0, 30.0);
		password = [[UITextField alloc] initWithFrame:rect];
		password.textColor = [UIColor colorWithRed:.0 green:.494 blue:.408 alpha:1];
		password.secureTextEntry = YES;
		password.returnKeyType = UIReturnKeyDone;
		password.keyboardType = UIKeyboardTypeNumberPad;
		password.tag = 1;
		[[self contentView] addSubview:password];
		[password release];
		
		self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField 
{
    NSLog(@"%@ textFieldShouldReturn", [self class]);
    [theTextField resignFirstResponder];
    // do stuff with the text
    NSLog(@"text = %@", [theTextField text]);
    return YES;
}

- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Dismiss the keyboard when the view outside the text field is touched.
	[password resignFirstResponder];
	
	
}
When I touch inside of the cell in an area that is not the textField the touchesBegin is called and the keypad disappears. When I add the same code to the GeneralSettings.m it does not work... Here is what is in my GeneralSettings.m

(This never ends up getting called in the GeneralSettings)
Code:
- (BOOL)textFieldShouldReturn:(UITextField *)theTextField 
{
    NSLog(@"%@ textFieldShouldReturn", [self class]);
    [theTextField resignFirstResponder];
    // do stuff with the text
    NSLog(@"text = %@", [theTextField text]);
    return YES;
}
Code:
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    // Dismiss the keyboard when the view outside the text field is touched.
	//[self.view.tableView.password resignFirstResponder];
	NSLog(@"You touched somewhere!");
}
-(IBAction) backgroundClick:(id)sender 
{
	[sender resignFirstResponder]; 
	NSLog(@"Here I am");
}
Niether of those functions ever gets called. I can click anywhere inside the view and I get nothing.... also even if they did get called I dont know how I would access the text field in a custom cell in the table view... I cant seem to even get access to the tableView (self.tableView doesnt work)

Am I doing something totally wrong here? I hope this code helps.
pcmofo is offline   Reply With Quote
Old 01-16-2009, 02:32 PM   #6 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 91
pcmofo is on a distinguished road
Default

Any ideas?
pcmofo is offline   Reply With Quote
Old 01-19-2009, 09:24 AM   #7 (permalink)
Registered Member
 
Join Date: Oct 2008
Posts: 91
pcmofo is on a distinguished road
Default

Still no luck with this after searching this and other forums all week long... I would really appreciate it if someone could point me in the right direction.
pcmofo is offline   Reply With Quote
Old 01-19-2009, 03:02 PM   #8 (permalink)
New Member
 
Join Date: Jan 2009
Posts: 3
pbcaus is on a distinguished road
Default Possibilities

There are a couple I things I can see from the code.[list=1]
  1. I see you are using two different cell identifiers: "SettingsViewController" and "PasswordTableCell". This won't cause the problem, but it means you are taking a performance hit as it creates a new cell everytime the user scrolls rather than reusing an existing cell.
  2. GeneralSettings is the delegate of the password field, not the cell.
  3. It would appear that you are releasing the password field prematurely.
Given that password is a property, releasing it during the initialisation probably isn't helping your cause. It will appear on the screen, given the addSubview earlier, but anything can happen in the code after that point and you are never really sure when it was actually released. If you access the property, you don't know where the property is pointing and this can lead to memory corruption, which is a really nasty bug to track down.

I hope this helps.
pbcaus is offline   Reply With Quote
Old 03-19-2011, 07:04 AM   #9 (permalink)
Registered Member
 
Join Date: Mar 2011
Posts: 2
vijaydogra is on a distinguished road
Default Multiple UITextFields and resignFirstResponder

I know this is pretty old topic, however, even when I stumbled upon this issue I found that there was no satisfactory answers anywhere. So here is my workaround (note that this is not solution)

We always have the last object of textfield, using this I made some changes and did this...
Code:
[[cell textField] becomeFirstResponder];
[[cell textField] resignFirstResponder];
This way I forcibly made the last text field that we have to becomeFirstResponder committing the changes in the desired textField and then resigning it as responder ... VOILA!!!! Issue solved for me.
vijaydogra is offline   Reply With Quote
Old 02-06-2012, 06:09 AM   #10 (permalink)
Registered Member
 
Join Date: Feb 2012
Posts: 1
robbash is on a distinguished road
Default

Are you displaying the view modally?

Maybe this helps then:
ipad - Modal Dialog Does Not Dismiss Keyboard - Stack Overflow
robbash 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: 403
13 members and 390 guests
13dario13, 7twenty7, buggen, EvilElf, glenn_sayers, j.b.rajesh@gmail.com, LunarMoon, morterbaher, QuantumDoja, sacha1996, Sami Gh, VinceYuan
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,673
Threads: 94,122
Posts: 402,906
Top Poster: BrianSlick (7,990)
Welcome to our newest member, morterbaher
Powered by vBadvanced CMPS v3.1.0

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