Ios 关闭文本字段上的键盘-自定义表格单元格
我有一个自定义单元格,在自定义单元格中,我有一个文本字段,用户可以在其中更改值。不管我怎么做,当用户输入一些值时,我都无法关闭键盘。它不会影响下面显示的任何委托方法 CheckOutTableViewCell.mIos 关闭文本字段上的键盘-自定义表格单元格,ios,objective-c,uitableview,Ios,Objective C,Uitableview,我有一个自定义单元格,在自定义单元格中,我有一个文本字段,用户可以在其中更改值。不管我怎么做,当用户输入一些值时,我都无法关闭键盘。它不会影响下面显示的任何委托方法 CheckOutTableViewCell.m #import "CheckOutTableViewCell.h" @implementation CheckOutTableViewCell @synthesize productName; @synthesize productPrice; @synthesize product
#import "CheckOutTableViewCell.h"
@implementation CheckOutTableViewCell
@synthesize productName;
@synthesize productPrice;
@synthesize productOrderNumberTF;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
-(id) init;
{
self = [super init];
if (!self) return nil;
productOrderNumberTF.delegate = (id)self;
return self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:@"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"%@", textField.text);
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
// dismiss keyboard when user clicks on anywhere on the UI
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.productOrderNumberTF resignFirstResponder];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
CheckOutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[CheckOutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.productOrderNumberTF.delegate = (id)self;
}
return cell
}
CheckOutTableViewController.m
#import "CheckOutTableViewCell.h"
@implementation CheckOutTableViewCell
@synthesize productName;
@synthesize productPrice;
@synthesize productOrderNumberTF;
- (void)awakeFromNib {
[super awakeFromNib];
// Initialization code
}
-(id) init;
{
self = [super init];
if (!self) return nil;
productOrderNumberTF.delegate = (id)self;
return self;
}
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
// enter closes the keyboard
if ([string isEqualToString:@"\n"])
{
[textField resignFirstResponder];
return NO;
}
return YES;
}
- (void) textFieldDidEndEditing:(UITextField *)textField {
NSLog(@"%@", textField.text);
}
- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
[textField resignFirstResponder];
return YES;
}
// dismiss keyboard when user clicks on anywhere on the UI
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
[self.productOrderNumberTF resignFirstResponder];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"cell";
CheckOutTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil)
{
cell = [[CheckOutTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
cell.productOrderNumberTF.delegate = (id)self;
}
return cell
}
ViewController的屏幕截图
您的
委托
方法未调用,因为您的文本字段委托
值未赋值
在awakeFromNib
- (void)awakeFromNib {
[super awakeFromNib];
productOrderNumberTF.delegate = self;
}
nib加载基础结构向从nib存档重新创建的每个对象发送一条消息,但只有在存档中的所有对象都已加载并初始化之后。当一个对象收到awakeFromNib消息时,它保证已经建立了所有的出口和操作连接
删除cell.productOrderNumberTF.delegate=(id)self代码>来自您的
签出TableViewController.m
类。它是在TableCell.m
还是TableViewController.m
中?