2009-12-22 61 views
2

因此,在最終學習如何將用戶輸入存儲到一個變量後,我意識到使用小數輸入數字是相當困難的。數字鍵盤不允許小數部分,如果我使用數字&標點符號,它看起來有點奇怪。用iPhone SDK輸入十進制數字的最佳方法是什麼?

所以我必須使用數字選取器來平滑地允許用戶輸入數字嗎?或者我應該使用數字&標點符號,因爲我能夠學習如何存儲該輸入(幾乎)?

任何幫助將不勝感激。

+0

這裏有一個類似的問題是有一些很好的答案:http://stackoverflow.com/questions/276382/best-way-to-enter-numeric-values-with-decimal-points – 2009-12-22 21:11:39

+1

耶我看到了這個。 OP從小數開始,然後帖子轉移到將數字更改爲貨幣。我不需要用小數點進行貨幣轉換,我需要允許用戶輸入3454.43434343例如 – HollerTrain 2009-12-22 21:16:03

回答

2

您可以隨時按照用於數字鍵盤上的完成按鈕的same philosophy。基本上,您在左下角的空白區域中創建自己的小數點按鈕。

只需按照添加和監控按鈕的指示。唯一需要更改的將是@selector(doneButton:)@selector(decimalButton:)。您的十進制按鈕的方法是:

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

TY作爲您的答覆。哇,看起來像一些工作在那裏:)不知道哪一個會更容易:做一個修改鍵盤或學習如何將UIPicker數據轉換成變量....蘋果允許在審查應用程序時修改鍵盤? – HollerTrain 2009-12-22 21:22:46

+0

哈哈,如果你知道Photoshop,不應該太難。 ;)無論選擇哪個選項都取決於您的應用程序。有一點需要記住的是用戶討厭被限制在一組特定的選項中。 是的,Apple幾乎允許任何不使用任何私有API的東西。我現在有一個應用程序,它的子類'UIAlertView'添加一個'UITextField'沒有任何問題。而在你的情況下,你所做的只是製作一個'UIButton',它會發生*隨鍵盤移動! – 2009-12-22 21:31:24

+0

是的,我的應用程序是如此簡單,它可能需要大約兩秒的每個人這個論壇。我只需要用戶輸入帶小數的數字的能力。所以我可能會刺你的想法。我真的很感謝你的幫助克里斯,並且不要驚訝地看到我在這裏再次提出關於我的痛苦簡單的應用程序的另一個問題:) – HollerTrain 2009-12-22 21:33:21

0

如果你不想去與克里斯的回答,您可以通過實施使用數字和標點符號鍵,然後「禁用」的所有非數字/小數點輸入的方法UITextFieldDelegate

1

對於我的應用程序,我推出了我自己的數字鍵盤。創建一個模式的看法與我需要的所有按鈕,當我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

+0

是否有機會發布如何設置客戶鍵盤的示例? – Tim 2009-12-22 22:44:06

+0

謝謝托馬斯! – Tim 2009-12-23 00:54:55

0

在SDK 3.0版本說明,蘋果公司寫了如下警告:

不要利用現有的鍵盤的頂部自定義按鈕。可能出現任何數量的繪圖,事件或兼容性問題。

現在這是真的,他們已經接受這樣一個按鈕添加在現有的鍵盤,但是該技術使用無證鏈接UIKeyboard查看並可以很容易地在提交下一次被拒絕的申請。從技術上講,它屬於未公開的API領域,但我不相信他們的自動化工具會將其提取出來,因此不會被標記。

你應該張貼在蘋果開發者網站上的錯誤,因爲他們在這個問題上計數錯誤報告的數量,才正式將解決這個問題...

有他們能解決方式數量這包括:

1)支持更多預定義的鍵盤選項(十進制,貨幣等)。 2)揭露UIKeyboard方法 3)提供定製,可如果你打算本地化您的應用程序進行本地化

鍵盤選項,你會當你覆蓋一個具有不同尺寸的鍵盤問題「」鍵入其中一個現有鍵盤上的空白區域(與該方法中的「完成」按鈕相同的問題)。

我目前使用覆蓋「。」在我自己的一個應用程序中,最終可能會創建一個完全自定義的「小數」鍵盤來替代此鍵盤,因此我不會冒險在未來的應用更新中被拒絕。

-t

相關問題