【问题标题】:UIKeyboardTypeDecimalPad with negative numbers带有负数的 UIKeyboardTypeDecimalPad
【发布时间】:2012-03-25 16:01:04
【问题描述】:

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

关于如何实现这一点的任何想法或想法?

【问题讨论】:

  • 键盘不支持负数,袖珍计算器不支持,用户必须在数字前加上-才能处理负数。
  • 好问题。你有没有得到比“在它旁边放一个 - 按钮”更好的答案?

标签: ios uitextfield uikeyboard uikeyboardtype


【解决方案1】:

您可以使用 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;

【讨论】:

    【解决方案2】:

    我不相信这样的事情现在是可能的(因为 Apple 还没有实现它)。唯一的选择是设计自己的键盘或使用完整的 ASCII 键盘。

    【讨论】:

      【解决方案3】:

      据我所知,苹果还没有实现这样的标准键盘。但是,可以将 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);
          }
      

      对不起,糟糕的代码格式,但你明白了 :)

      【讨论】:

        【解决方案4】:
        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)"
            }
        }
        

        【讨论】:

        • 简单且最佳的解决方案。
        • 如何将 plusMinusAction 传递给 UITextField
        【解决方案5】:

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

        希望这段代码对您有所帮助:

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

        NSString *fieldString = [NSString stringWithFormat:@"%@",Textfield.text];
        
                NSLog(@"%@",fString);
        
                int fieldValue;
                value = [fString intValue];
                NSLog(@"%d",fieldValue);
        

        这适用于十进制数

                    double fieldValue;
                    value = [fString doubleValue];
                    NSLog(@"%f",fieldValue);
        

        【讨论】:

        • 这并不能解决问题,因为用户无法从十进制键盘输入“-”。那基本不可能了?
        • @Hugo T 我在某处看到了一个教程,在十进制键盘上用 - 标记了空按钮。如果我能在某个地方找到它,我会看看。
        • 这适用于 3.5" 屏幕,但需要对 4" iPhone 5 进行一些修改,并且点必须更改为“-”(很简单)祝你好运。 brygruver.squarespace.com/blog/2009/10/1/…
        • 所有这些按钮建议的一个问题是,如果您在可访问性中打开了按钮形状,则会在按钮的文本下方绘制一条线(其余部分不会发生这种情况)键盘)。
        【解决方案6】:

        为了完成我使用带有文本字段的“.inputAccessoryView”来完成的任务

        在 viewDidLoad 上

        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;
        

        }

        这将在键盘上方添加一个自定义视图作为其中的一部分

        终于得到'减号'

        #pragma mark - IBActions
        
        - (IBAction)minusTouchUpInside:(id)sender
        {
        NSString *value = self.textField.text;
        if (value.length > 0) {
            NSString *firstCharacter = [value substringToIndex:1];
            if ([firstCharacter isEqualToString:@"-"]){
                self.textField.text = [value substringFromIndex:1];
            }else{
                self.textField.text = [NSString stringWithFormat:@"-%@", value];
            }
        }
        }
        
        - (IBAction)doneTouchUpInside:(id)sender
        {
            [self.textField resignFirstResponder];
        }
        

        【讨论】:

          猜你喜欢
          • 2012-02-12
          • 2011-09-15
          • 1970-01-01
          • 2023-03-24
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-04-06
          • 1970-01-01
          相关资源
          最近更新 更多