gpt4 book ai didi

objective-c - Obj-c/ cocoa touch : Have UITableViewCell - CellTextField only accept Numeric chars?

转载 作者:行者123 更新时间:2023-11-29 04:28:58 25 4
gpt4 key购买 nike

我希望 UITableViewCell 中的 CellTextField 仅接受数字字符。我以编程方式创建 UITableViewCells,如下所示:

    -(UITableViewCell*) getCellContentView: (NSString*) cellIdentifier {

...

UITableViewCell *cell = [[UITableViewCell alloc] initWithFrame:cellFrame reuseIdentifier:cellIdentifier];

...

CellTextField *txt;
txt = [[CellTextField alloc] initWithFrame:amountFrame];

... //here we should setup a delegate or similar to handle char validation for txt

[cell.contentView addSubview:txt];
return cell;
}

对于 UITextField 组件,我们有一个委托(delegate)“shouldChangeCharactersInRange”,但我找不到 CellTextFields 的等效项。

最佳答案

试试这个

.h

  @interface YourClass : ParentClass <UITextFieldDelegate>{
NSNumberFormatter *integerNumberFormatter;
}
@end

.m

-(void)viewDidLoad
{
[integerNumberFormatter setMaximumFractionDigits:0];
yourTextField.delegate = self;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {

NSString* proposedString = [textField.text stringByReplacingCharactersInRange:range withString:string];
NSNumber *proposedNumber = [integerNumberFormatter numberFromString:proposedString];
if (proposedNumber) {
NSString *integerString = [integerNumberFormatter stringFromNumber:proposedNumber];
if ([integerString isEqualToString:proposedString]) {
// proposed string is an integer
return YES;
}
}

return NO;
}

关于objective-c - Obj-c/ cocoa touch : Have UITableViewCell - CellTextField only accept Numeric chars?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11967937/

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