我正在开发一款iOS应用,要求用户使用键盘类型UIKeyboardTypeDecimalPad在UITextField中输入数字。但是我只是意识到没有支持输入负数,这是应用程序的要求。
关于如何实现这一目标的任何想法或想法?
答案 0 :(得分:3)
您可以使用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;
答案 1 :(得分:2)
我不相信现在这样的事情是可能的(因为Apple还没有实现它)。唯一的选择是设计自己的键盘或使用完整的ASCII键盘。
答案 2 :(得分:1)
据我所知,苹果还没有实现这样的标准键盘。但是,可以将UIButtons添加到任何键盘窗口。 this link should help或类似教程this link should also help
基本上你注册一个NSNotificationListener来监听键盘出现。抓住键盘框架并在其视图中添加UIButton。上面的链接不是我们想要的,但它是正确的想法。
在appDelegate中,
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
- (void)keyboardDidShow:(NSNotification *)note
{
// Get the Very Top Window on the Display. That's where the Keyboard is.
NSInteger topWindow = [[[UIApplication sharedApplication] windows] count] - 1;
UIWindow *keyboard = [[[UIApplication sharedApplication] windows] objectAtIndex:topWindow];
// If the dot has not been created (first time the keyboard has been displayed) create it.
if (self.dot == nil)
{
self.dot = [UIButton buttonWithType:UIButtonTypeCustom];
// Make the dot a subview of the view containing the keyboard.
[keyboard addSubview:self.dot];
// Place the dot in the correct location on the keyboard.
[self.dot setFrame:CGRectMake(0, 427, 106, 53)];
// Set the overlay graphics. (Use TransDecimalDown.png and TransDecimalUp.png for the Alert Style Keyboard.
[self.dot setImage:[UIImage imageNamed:@"DecimalUp.png"] forState:UIControlStateNormal];
[self.dot setImage:[UIImage imageNamed:@"DecimalDown.png"] forState:UIControlStateHighlighted];
// Give the dot something to do when pressed.
[self.dot addTarget:self action:@selector(sendDecimal:) forControlEvents:UIControlEventTouchUpInside];
}
// Bring the dot to the front of the keyboard.
[keyboard bringSubviewToFront:self.dot];
}
- (void)sendDecimal:(id)sender {
// Post a notification that the dot has been pressed. Observing view controllers are then responsible for adding the actual decimal.
[[NSNotificationCenter defaultCenter] postNotificationName:@"DecimalPressed" object:nil];
// Play the Keyboard Click. If the user has these sound effects turned off, the decimal will still click. Sorry. :( (Also, doesn't seem to work on the simulator, no keyboard clicks seem to.)
AudioServicesPlaySystemSound(0x450);
}
对于可怕的代码格式感到抱歉,但您明白了这一点:)
答案 3 :(得分:0)
此代码可以帮助您:
如果你想要一个负数,只需使用“ - ”
NSString *fieldString = [NSString stringWithFormat:@"%@",Textfield.text];
NSLog(@"%@",fString);
int fieldValue;
value = [fString intValue];
NSLog(@"%d",fieldValue);
这适用于十进制数
double fieldValue;
value = [fString doubleValue];
NSLog(@"%f",fieldValue);
答案 4 :(得分:0)
为了完成任务,我使用'.inputAccessoryView'和textfield
在viewDidLoad
上componentWillMount: function () {
if (typeof window !== "undefined") {
// register listener in browser only
}
}
然后
self.textField.inputAccessoryView = [self accessoryViewForTextField:self.textField];
}
这将在键盘上方添加一个自定义视图作为其一部分
最终得到'减'
- (UIView *)accessoryViewForTextField:(UITextField *)textField{
UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 44)];
view.backgroundColor = [UIColor lightGrayColor];
UIButton *minusButton = [UIButton buttonWithType:UIButtonTypeCustom];
UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
[minusButton setTitle:@"-" forState:UIControlStateNormal];
[doneButton setTitle:NSLocalizedString(@"Done", @"Done") forState:UIControlStateNormal];
minusButton.backgroundColor = [UIColor magentaColor];
doneButton.backgroundColor = [UIColor blueColor];
CGFloat buttonWidth = view.frame.size.width/3;
minusButton.frame = CGRectMake(0, 0, buttonWidth, 44);
doneButton.frame = CGRectMake(view.frame.size.width - buttonWidth, 0, buttonWidth, 44);
[minusButton addTarget:self action:@selector(minusTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[doneButton addTarget:self action:@selector(doneTouchUpInside:) forControlEvents:UIControlEventTouchUpInside];
[view addSubview:minusButton];
[view addSubview:doneButton];
return view;
答案 5 :(得分:0)
func addToolbar() {
let toolbar = UIToolbar()
toolbar.sizeToFit()
let plusMinusButton = UIBarButtonItem(title: "+/-", style: .done, target: self, action: #selector(plusMinusAction))
plusMinusButton.tintColor = .black
plusMinusButton.width = UIScreen.main.bounds.width / 3
toolbar.items = [plusMinusButton]
toolbar.barTintColor = #colorLiteral(red: 0.7812563181, green: 0.8036255836, blue: 0.8297665119, alpha: 1)
toolbar.isTranslucent = false
myField.inputAccessoryView = toolbar
}
@objc func plusMinusAction() {
let text = myField.text ?? ""
if text.hasPrefix("-") {
myField.text = String(text.suffix(text.count - 1))
} else {
myField.text = "-\(text)"
}
}