gpt4 book ai didi

ios - 将 UITextfield 整数值发布到 Web 服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:01:51 25 4
gpt4 key购买 nike

我有一个 UITextField,我应该只能输入整数(无文本),然后必须将整数值传递给 NSMutableDictionary。然后我必须从这本词典发布到网络服务器。

最佳答案

UITextField 的键盘类型更改为 Number Pad,以便它只接受数字:

[textField setKeyboardType:UIKeyboardTypeNumberPad];

您还可以检查输入的文本是否为数字:

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
if (string.length == 0 || [NSScanner scanInt:string]) {
return YES;
}

return NO;
}

您可以使用以下代码将值保存在字典中:

//if you want to save as number
[myDictionary setObject:[NSNumber numberWithInt:[textField.text intValue] forKey:@"YourKey"];

//if you want to save as string
[myDictionary setObject:textField.text forKey:@"YourKey"];

关于ios - 将 UITextfield 整数值发布到 Web 服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25380887/

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