【问题标题】:What is Best Way to Input Decimal Numbers with iPhone SDK?使用 iPhone SDK 输入小数的最佳方法是什么?
【发布时间】:2009-12-22 21:02:24
【问题描述】:

所以在终于学会了如何将用户输入存储到变量中之后,我意识到让用户输入带有小数的数字是相当困难的。数字键盘不允许小数部分,如果我使用数字和标点符号,它看起来有点奇怪。

那么我必须使用数字选择器才能顺利让用户输入数字吗?或者我应该只使用数字和标点符号,因为我能够(几乎)学习如何存储该输入?

任何帮助将不胜感激。

【问题讨论】:

  • 这是一个类似的问题,有一些很好的答案:stackoverflow.com/questions/276382/…
  • 是的,我看到了。 OP以小数开头,然后帖子将数字变为货币。我不需要用小数进行货币转换我需要允许用户输入 3454.43434343 例如

标签: iphone


【解决方案1】:

您可以随时关注数字键盘上用于完成按钮的same philosophy。基本上,您可以在左下角的空白处制作自己的小数点按钮。

只需按照说明添加和监控按钮即可。您唯一需要更改的是将@selector(doneButton:) 更改为@selector(decimalButton:)。您的小数按钮方法是:

- (void)decimalButton: (id) sender {
     myTextField.text = [myTextField.text stringByAppendingString:@"."];
}

【讨论】:

  • 您的回复。哇,看起来那里涉及到一些工作 :) 想知道哪个会更容易:制作修改后的键盘或学习如何将 UIPicker 数据转换为变量......Apple 在审查应用程序时是否允许修改键盘?跨度>
  • 哈哈,懂 Photoshop 应该不会太难。 ;) 您选择的任何选项都取决于您的应用程序。要记住的一件事是,用户讨厌被限制在一组特定的选项中。是的,Apple 几乎允许任何不使用任何私有 API 的东西。我现在有一个应用程序子类UIAlertView 添加UITextField 没有任何问题。在你的情况下,你所做的只是创建一个UIButton,它碰巧随着键盘移动!
  • 是的,我的应用程序非常简单,它可能会花掉这个论坛的每个人大约两秒钟的时间。我只需要用户输入带小数的数字的能力。所以我可能会尝试一下你的想法。我真的很感谢你的帮助,克里斯,再次看到我在这里提出关于我痛苦的简单应用程序的另一个问题,不要感到惊讶:)
  • UIPickerViews 并不像看起来那么具有威胁性。您的视图控制器既可以是数据源也可以是委托,您几乎可以从 Apple 网站上的协议参考中复制和粘贴方法声明。
【解决方案2】:

对于我的应用,我推出了自己的数字键盘。用我需要的所有按钮创建了一个模态视图,当我的 UITextfield 子类成为第一响应者时,我将其动画化。

相当多的工作,但您也可以自定义外观和感觉(使用您自己的漂亮按钮等),这正是我想要的应用程序。

更新:我的 DecimalKeyboardViewController:

我实际上使用 UIControl 的子类而不是 UITextfield,因为我以某种自定义方式显示它。如果我要继承 UITextfield 我会做这样的事情:

@interface DecimalNumberTextfield : UITextfield {
    NSDecimalNumber *value;
}
@property (nonatomic, retain) NSDecimalNumber *value
@end

@implementation DecimalNumberTextfield
- (BOOL)becomeFirstResponder
{
    if ([super becomeFirstResponder]) {
        [[DecimalKeyboardViewController sharedDecimalKeyboardViewController] showForField:self];
        return YES;
    } else {
        return NO;
    }
}
- (BOOL)resignFirstResponder
{
    if ([super resignFirstResponder]) {
        [[DecimalKeyboardViewController sharedDecimalKeyboardViewController] hide];
        return YES; 
    } else {
        return NO;
    }
}
- (void)setValue:(NSDecimalNumber *)aValue {
    // you need to set value and update the displayed text here
}
@end

这里是 DecimalKeyboardViewController。您需要创建 nib 文件并向按钮添加标签(0-9 可能表示数字,其他标签表示您将使用的其他按钮),然后将按钮连接到 -buttonPressed::

@interface DecimalKeyboardViewController : UIViewController {
    DecimalNumberTextField *fieldBeingEdited;
}
@property (nonatomic, retain) DecimalNumberTextField *fieldBeingEdited;

+ (id)sharedDecimalKeyboardViewController;
- (void)showForField:(DecimalNumberTextField *)aTextField;
- (void)hide;
- (IBAction)buttonPressed:(id)sender;
@end

@implementation DecimalKeyboardViewController
static DecimalKeyboardViewController *sharedViewController;

+ (id)sharedDecimalKeyboardViewController
{
    if (!sharedViewController) {
        sharedViewController = [[DecimalKeyboardViewController alloc]
                                initWithNibName:@"DecimalKeyboardViewController"
                                         bundle:[NSBundle mainBundle]];
    }
    return sharedViewController;
}
- (void)showForField:(DecimalNumberTextField *)aTextField
{
    self.fieldBeingEdited = aTextField;

    // add ourselves to the window unless we're there already
    UIWindow *theWindow = self.fieldBeingEdited.window;
    CGRect frame = self.view.frame;

    if (!self.view.superview) { // add ourselves to the UIWindow unless we're already visible
        frame.origin.y = theWindow.frame.size.height; // we start just out of sight at the bottom
        self.view.frame = frame;
        [theWindow addSubview:self.view];
    }

    // start animating
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    // animate the keyboard in
    frame.origin.y = theWindow.frame.size.height - frame.size.height;
    self.view.frame = frame;

    // GO!
    [UIView commitAnimations];
}
- (void)hide
{
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationBeginsFromCurrentState:YES];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];

    if (self.view.superview) {
        CGRect frame = self.view.frame;
        frame.origin.y = self.view.superview.frame.size.height;
        self.view.frame = frame;
    }

    [UIView commitAnimations];

    self.fieldBeingEdited = nil;    
}
- (IBAction)buttonPressed:(id)sender
{
    UIButton *button = (UIButton *)sender;

    NSDecimalNumber *oldNumber = self.fieldBeingEdited.value;
    NSDecimalNumber *newNumber;

    switch (button.tag) {
        // calculate the new number based on what button the user pressed
    }

    // update the DecimalNumbertextfield
    self.fieldBeingEdited.value = newNumber;
}
@end

更新 2:由于我将 UIControl 子类化,因此我不必处理弹出的标准键盘。如果您将UITextfield 子类化,我认为也会显示普通键盘。您必须以某种方式(不确定如何)或像我一样将UIControl 子类化。

【讨论】:

  • 有机会发布您如何设置客户键盘的示例吗?
【解决方案3】:

如果您不想接受 Chris 的回答,您可以使用数字和标点符号键盘,然后通过实现 UITextFieldDelegate 的方法“禁用”所有非数字/小数点输入

【讨论】:

    【解决方案4】:

    在 SDK 3.0 发行说明中,Apple 写了以下警告:

    不要在现有键盘上绘制自定义按钮。可能会出现任何数量的绘图、事件或兼容性问题。

    现在是真的,他们一直在接受在现有键盘上添加按钮的应用程序,但是这种技术使用 UIKeyboard 视图的未记录链接,并且在下次提交时很容易被拒绝。从技术上讲,它属于未记录的 API 领域,但我不相信他们的自动化工具会发现它,所以它不会被标记。

    您应该在 Apple Developer Connection 网站上发布错误,因为他们会在正式解决此问题之前计算有关此问题的错误报告的数量...

    他们可以通过多种方式解决此问题,包括:

    1) 支持更多预定义的键盘选项(十进制、货币等)。 2) 公开 UIKeyboard 方法 3) 提供可本地化的自定义键盘选项

    如果您打算本地化您的应用程序,当您覆盖“。”时,您将遇到不同键盘大小的问题。在现有键盘之一的空白处键入(与该方法中的“完成”按钮具有相同的问题)。

    我目前正在使用覆盖的“。”在我自己的一个应用程序中,并且可能最终会创建一个完全自定义的“十进制”键盘来替换它,这样我就不会冒险在未来的应用程序更新中被拒绝。

    -t

    【讨论】:

      猜你喜欢
      • 2010-09-21
      • 1970-01-01
      • 2013-02-03
      • 2010-09-15
      • 2010-10-07
      • 1970-01-01
      • 2011-12-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多