gpt4 book ai didi

iphone - 带有 TextField 的 UITableViewCell 并检查输入

转载 作者:行者123 更新时间:2023-11-28 20:42:52 25 4
gpt4 key购买 nike

我目前正在创建一个带有 UITextField 的 UITableViewCell。单击文本字段时,我想调出我创建的数字键盘。当我输入时,文本字段应该为我检查输入;点击其他地方,键盘应该被关闭。

代码:

UITableViewCell *sizeCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"sizeCell"];
sizeCell.textLabel.text = @"Size";
UITextField* sizeField = [[UITextField alloc] initWithFrame:CGRectMake(185, 10, 100, 28)];
sizeField.text = @"0";
sizeField.textAlignment = UITextAlignmentRight;
sizeField.textColor = [UIColor colorWithRed:50.0/255.0 green:79.0/255.0 blue:133.0/255.0 alpha:1.0f];
sizeField.backgroundColor = [UIColor clearColor];
sizeField.keyboardType = UIKeyboardTypeDecimalPad;

[sizeCell.contentView addSubview:sizeField];

rows = [[NSArray arrayWithObjects:switchCell, typeCell, sizeCell, nil] retain];

我尝试像这样实现 UITextFieldDelegate:

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
[sizeField resignFirstResponder];
return YES;
}

但是键盘并没有消失...

如何验证输入并关闭键盘?

最佳答案

您永远不会在文本字段上设置委托(delegate),以便调用 textFieldShouldReturn:。确保您的类符合 UITextFieldDelegate,然后执行以下操作:

...
UITextField* sizeField = [[UITextField alloc] initWithFrame:CGRectMake(185, 10, 100, 28)];
sizeField.delegate = self; //This is important!
sizeField.text = @"0";
...

关于iphone - 带有 TextField 的 UITableViewCell 并检查输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7692564/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com