2011-04-29 134 views
9

我是iPhone技術的新手。我在iPhone應用程序中保存了一個問題,但是當我使用文本字段鍵盤類型數字鍵盤時,鍵盤不顯示返回/完成按鈕。數字鍵盤鍵盤不顯示返回鍵中的iPhone

如何在文本框中輸入數字後隱藏數字鍵盤鍵盤?你有什麼解決方案。請提供幫助。

回答

11

你可以這樣做:

@property (nonatomic, strong) UITextField *textField; 

- (void)viewDidLoad { 
    [super viewDidLoad]; 

    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, 100, 40)]; 
    UIBarButtonItem *doneItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonDidPressed:)]; 
    UIBarButtonItem *flexableItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL]; 
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, [[UIScreen mainScreen] bounds].size.width, [[self class] toobarHeight])]; 
    [toolbar setItems:[NSArray arrayWithObjects:flexableItem,doneItem, nil]]; 
    self.textField.inputAccessoryView = toolbar; 
} 

- (void)doneButtonDidPressed:(id)sender { 
    [self.textField resignFirstResponder]; 
} 

+ (CGFloat)toolbarHeight { 
    // This method will handle the case that the height of toolbar may change in future iOS. 
    return 44.f; 
} 
+2

對於iOS 6.1,這不工作,除非我初始化工具欄如下所示:'UIToolbar * toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0,0,self.view.bounds.size.width,44)];'否則,完美答案 – Segfault 2013-04-22 19:24:12

+0

@Segfault確認。您需要指定一個框架才能在6.1中正常工作。 – Paul 2013-06-22 12:40:13

+0

感謝Segfault,我會編輯它。 – Oscar 2013-07-19 03:12:00

1

數字鍵盤沒有返回鍵或完成鍵。當用戶觸摸文本框外部時,您可以做一件事情,您可以隱藏鍵盤。你應該做這樣的事情 -

if (there is a touch outside of your textField) 
{ 

    [textField resignFirstResponder]; 

} 
+0

但我想在數字鍵盤上創建一個完成按鈕,所以如何在數字鍵盤上創建完成按鈕。如果它是可行的,那麼請提供幫助。 – jaydev 2011-05-01 06:42:45

+0

對不起,但這是不可能與iPhone sdk使用私人API'你必須去一些替代解決方案,你可以創建自己的鍵盤上完成按鈕.. – Saurabh 2011-05-01 09:23:19

6

您必須使用UIKeyboardTypeNumberPad,試試這個,而不是UIKeyboardTypeNumbersAndPunctuation, 這將不僅顯示了返回鍵,還爲您提供一些額外的標點符號

0

這個問題有已經回答了,我知道,因爲那是我如何得到解決方案。您有2個選項,首先是通過在主視圖上觸摸來發送「完成編輯」消息來隱藏鍵盤。隱藏鍵盤[self.view endEditing:YES];

如果您將觸摸監聽器添加到主視圖中,則必須實現一個條件,以便其他任何按鈕保持工作狀態。

你想這樣做,以模擬返回鍵有什麼實際添加這樣的:

註冊鍵盤確實表現出通知,將其添加到代碼:

if ([self.passCodeLabel isFirstResponder]) 
     { 
      UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
      doneButton.frame = CGRectMake(0, 163, 106, 53); 
      //doneButton.frame = CGRectMake(0, 163, 257, 257); 
      doneButton.adjustsImageWhenHighlighted = NO; 
      [doneButton setImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal]; 
      [doneButton setImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted]; 
      [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 

      // locate keyboard view 
      UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
      NSLog(@"%@",[[UIApplication sharedApplication] windows]); 

      UIView* keyboard; 
      NSLog(@"Shared applicaiton windows count:%i",tempWindow.subviews.count); 
      for(int i=0; i<[tempWindow.subviews count]; i++) { 
       keyboard = [tempWindow.subviews objectAtIndex:i]; 
       NSLog(@"%@",[keyboard description]); 
       // keyboard view found; add the custom button to it 
       if([[keyboard description] hasPrefix:@"<UIPeripheralHostView"] == YES) 
       { 
        NSLog(@"Adding return button"); 
        [keyboard addSubview:doneButton]; 
       } 
      } 
     } 

這將將您自己的「完成」按鈕圖像添加到鍵盤(您可以通過截取空白插槽的屏幕截圖並添加完成的文本進行復制)。

順便說一句我粘貼的代碼在我的佈局上工作。對於你的你可能需要修改一下,但原理是一樣的。

  • 創建按鈕

  • 查找鍵盤子視圖

  • 按鈕添加到子視圖

0
- (void)viewDidLoad 
{ 

    [super viewDidLoad]; 
    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)]; 
    numberToolbar.barStyle = UIBarStyleBlackTranslucent; 
    numberToolbar.items = [NSArray arrayWithObjects: 
          [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil], 
          [[UIBarButtonItem alloc]initWithTitle:@"Done" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)], 
          nil]; 
    [numberToolbar sizeToFit]; 

    _txt_mobileno.inputAccessoryView = numberToolbar; 

} 

-(void)doneWithNumberPad 
{ 
    [_txt_mobileno resignFirstResponder]; 

}