Quote:
Originally Posted by pbcaus
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.