【发布时间】:2012-05-06 15:15:56
【问题描述】:
我有一个包含高度值的 UITextField。我想在用户输入 UITextField 时格式化字段的值。例如如果我想输入值为“5 ft 10”,流程将是:
1. Enter 5
2. " ft " is appended immediately after I type 5 with leading & trailing space.
我的代码如下所示:
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if ( [string isEqualToString:@""] ) return YES;
// currHeight Formatting
if ( textField == currHeight ) {
if (currHeight.text.length == 1) {
currHeight.text = [NSString stringWithFormat:@"%@ ft ", currHeight.text];
}
}
return YES;
}
我卡在输入 5 的位置,没有任何反应。我必须点击任何按钮才能附加“ft”。
我可以在不点击任何东西的情况下执行此操作吗?
【问题讨论】:
-
+1 @Durga。另见stackoverflow.com/questions/2198067/…
标签: iphone ios uitextfield uitextfielddelegate