我正在尝试创建一个UITextField,用户可以在1-10之间输入一个数字,以便给出10分。例如,5 / 10,9 / 10等。
理想情况下,我希望用户只能输入1到10,并显示为x / 10。其中x是用户输入的分数。
我一直在使用:
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
但我不确定下一步该做什么。任何帮助将不胜感激。
约什
答案 0 :(得分:2)
请尝试以下代码,希望它能满足您所需的所有条件
- (BOOL)textField:(UITextField*)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString*)string
{
NSString *resultingString = [textField.text stringByReplacingCharactersInRange: range withString: string];
if (resultingString.length > 2 && range.length == 0)
{
return NO;
}
else
{
if (resultingString.length<=1) {
return YES;
}
else
{
NSString *firstDigit = [resultingString substringToIndex:1];
NSString *secondDigit = [resultingString substringFromIndex:1];
if ([firstDigit intValue]>1) {
return NO;
}
else if ([secondDigit intValue]>0)
{
if ([firstDigit intValue]==0) {
return YES;
}
return NO;
}
else
{
return YES;
}
}
}
}
答案 1 :(得分:0)
试试这个
这样您只能输入1-10范围内的数字
#define NUMBERS_ONLY @"1234567890"
#define CHARACTER_LIMIT 2
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
NSUInteger newLength = [textField.text length] + [string length] - range.length;
NSCharacterSet *cs = [[NSCharacterSet characterSetWithCharactersInString:NUMBERS_ONLY] invertedSet];
NSString *filtered = [[string componentsSeparatedByCharactersInSet:cs] componentsJoinedByString:@""];
NSString *myTextField = [textField.text stringByAppendingString:filtered];
return (([string isEqualToString:filtered])&&(newLength <= CHARACTER_LIMIT)&&(myTextField.integerValue<=10));
}
答案 2 :(得分:-1)
试试这个
这将限制文本字段只输入一个字符
public void Export_ex(string elem)
{
string elements = elem;
HttpContext.Current.Response.ContentType = "data:application/vnd.ms-excel";
HttpContext.Current.Response.AddHeader("content-disposition", "attachment; filename=Print.xls");
HttpContext.Current.Response.Write("<html xmlns:x=\"urn:schemas-microsoft-com:office:excel\">");
HttpContext.Current.Response.Write("<head>");
HttpContext.Current.Response.Write("<div>");
HttpContext.Current.Response.Write("<META http-equiv=\"Content-Type\" content=\"text/html; charset=utf- 8\">");
HttpContext.Current.Response.Write("<!--[if gte mso 9]><xml>");
HttpContext.Current.Response.Write("<x:ExcelWorkbook>");
HttpContext.Current.Response.Write("<x:ExcelWorksheets>");
HttpContext.Current.Response.Write("<x:ExcelWorksheet>");
HttpContext.Current.Response.Write("<x:Name>Report Data</x:Name>");
HttpContext.Current.Response.Write("<x:WorksheetOptions>");
HttpContext.Current.Response.Write("<x:ValidPrinterInfo/>");
HttpContext.Current.Response.Write("</x:Print>");
HttpContext.Current.Response.Write("</x:WorksheetOptions>");
HttpContext.Current.Response.Write("</x:ExcelWorksheet>");
HttpContext.Current.Response.Write("</x:ExcelWorksheets>");
HttpContext.Current.Response.Write("</x:ExcelWorkbook>");
HttpContext.Current.Response.Write("</xml>");
HttpContext.Current.Response.Write("<![endif]--> ");
HttpContext.Current.Response.Write("</head>");
HttpContext.Current.Response.Write(elements);
HttpContext.Current.Response.Flush();
// HttpContext.Current.Response.End();
}
并将键盘类型设置为数字键盘,以便仅允许用户输入数字,用户只能输入0到9之间的数字。
希望它有所帮助。