带有负数的 UIKeyboardTypeDecimalPad

SNV*_*NV7 7 uitextfield uikeyboard ios uikeyboardtype

我正在开发一个 iOS 应用程序,它要求用户使用键盘类型 UIKeyboardTypeDecimalPad 在 UITextField 中输入数字。然而我刚刚意识到不支持输入负数,这是应用程序的要求。

关于如何实现这一目标有什么想法或想法吗?

use*_*195 5

您可以使用 UIToolbar 作为 UITextField 的输入附件视图,并放置一个带有“+/-”(加号/减号)的按钮。

UIToolbar *toolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, self.view.bounds.size.width, 44)];
UIBarButtonItem *plusMinusBbi = [[UIBarButtonItem alloc]initWithTitle:@"+/-" style:UIBarButtonItemStylePlain target:self action:@selector(togglePositiveNegative:)];
toolbar.items = @[plusMinusBbi];
self.textField.inputAccessoryView = toolbar;
Run Code Online (Sandbox Code Playgroud)


Flo*_*497 0

这显然不是我的最佳答案..但我无法删除它,因为它已被接受。

这段代码可能会帮助你:

如果你想要一个负数,只需使用“-”

NSString *fieldString = [NSString stringWithFormat:@"%@",Textfield.text];

        NSLog(@"%@",fString);

        int fieldValue;
        value = [fString intValue];
        NSLog(@"%d",fieldValue);
Run Code Online (Sandbox Code Playgroud)

这适用于十进制数

            double fieldValue;
            value = [fString doubleValue];
            NSLog(@"%f",fieldValue);
Run Code Online (Sandbox Code Playgroud)