2012-07-27 123 views
0

我有一個佈局,我有兩個文本字段,一個是字母輸入,另一個是數字輸入。我爲這些鍵盤設置了不同類型的鍵盤,如字母文本字段的缺省值和數字字段的數字。如何爲數字文本字段和字母文本字段設置鍵盤?

我在數字鍵盤上添加了一個完成按鈕。仍然我遇到這個鍵盤問題,當我先錄製數字文本,然後顯示已完成按鈕鍵盤,而我先錄製字母文本,然後錄製在數字文本字段,然後它不會在數字鍵盤上添加完成按鈕。

我該如何解決?

我已經使用這些代碼在數字鍵盤上添加按鈕。

-(void)viewWillAppear:(BOOL)animated 
     { 
     // Register for the events 

     [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector (keyboardDidShow:) name: UIKeyboardDidShowNotification object:nil]; 
     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector (keyboardDidHide:) name: UIKeyboardDidHideNotification object:nil]; 

     keyboardVisible = NO; 
     scroll_view.contentSize=CGSizeMake(320, 400); 
     } 



     - (void)keyboardDidShow:(NSNotification *)note { 
     UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
     UIView* keyboard; 
     for (keyboard in tempWindow.subviews) { 
      if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
       if (numberPadShowing) { 
        [self addButtonToKeyboard]; 
        return;     
        break; 
       } else { 
        for (UIView *v in [keyboard subviews]){ 
         if ([v tag]==123) 
          [v removeFromSuperview]; 
        } 
       } 
     } 
    } 

    - 

    (void) keyboardDidHide: (NSNotification *)notif 
    { 
     // Is the keyboard already shown 
     if (!keyboardVisible) { 
      return; 
     } 
     // Keyboard is no longer visible 
     keyboardVisible = NO; 
    } 

    -(void)doneButton 
    { 
    UIScrollView *scrollView =[[UIScrollView alloc] init]; 
    scrollView=scroll_view; 
    [scrollView setContentOffset:svos animated:YES]; 
    [myActiveTextField resignFirstResponder]; 
    } 

     - (void)addButtonToKeyboard { 
     // create custom button 
     UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
     doneButton.frame = CGRectMake(0, 163, 106, 53); 
     doneButton.adjustsImageWhenHighlighted = NO; 
     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) { 
      [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal]; 
      [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted]; 
     } else {   
      [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]; 
     UIView* keyboard; 
     for(int i=0; i<[tempWindow.subviews count]; i++) { 
      keyboard = [tempWindow.subviews objectAtIndex:i]; 
      // keyboard found, add the button 
      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) { 
       if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) 
        [keyboard addSubview:doneButton]; 
      } else { 
       if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES) 
        [keyboard addSubview:doneButton]; 
      } 
     } 
    } 

     - (void)textFieldDidBeginEditing:(UITextField *)textField 
     { 
     myActiveTextField=textField; 
     if (myActiveTextField==txt_hour_section || myActiveTextField==txt_minute_section || myActiveTextField==txt_transport_time){ 
      numberPadShowing=TRUE; 
      UIScrollView *scrollView =[[UIScrollView alloc] init]; 
      scrollView=scroll_view; 

      svos = scrollView.contentOffset; 
      CGPoint pt; 
      CGRect rc = [textField bounds]; 
      rc = [textField convertRect:rc toView:scrollView]; 
      pt = rc.origin; 
      pt.x = 0; 
      pt.y -= 60; 
      [scrollView setContentOffset:pt animated:YES];   

     } 
     else{ 
      numberPadShowing=FALSE; 
     } 
    } 



    -(BOOL)textFieldShouldReturn:(UITextField *)textField 
    { 
     UIScrollView *scrollView =[[UIScrollView alloc] init]; 
     scrollView=scroll_view; 
     [scrollView setContentOffset:svos animated:YES]; 
     [textField resignFirstResponder]; 
     return YES; 
    } 

感謝在進步......

回答

1

我覺得你的問題是監守你正在利用阿爾法後場攻到數字領域,阿爾法鍵盤已經在視圖。因此,當您點擊數字字段(同時alpha鍵盤仍在視圖中)時,它不會觸發'keyboardDidShow'事件,因爲鍵盤已經從前一個字段顯示(儘管是alpha字符)。

嘗試將您的完成按鈕邏輯放入代表方法,用於編號字段的textfield'textFieldDidBeginEditing'事件。

0

如果我的第一個答案在您的實現中不起作用,那麼您可能需要考慮重構並設置UITextField的inputAccessoryView。代碼沿着這些線應該做的伎倆(你可以把它放在你的viewDidLoad筆尖,也注意到你需要自己實現doneTapped方法);

// Create a UIToolbar object and set its style to be black and transparent 
    numericInputAccessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)]; 
    numericInputAccessoryView_.barStyle = UIBarStyleBlackTranslucent; 

// The flexible space item will push the done button to the far right 
    UIBarButtonItem *space = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil]; 

// Make the done button, this is a system item so you don't need to set the title or anything. This will call a method called "doneTapped:(id)sender" when you tap it, you'll need to implement that yourself. 
    UIBarButtonItem *done = [[UIBarButtonItem alloc]WithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)]; 

// Stick the flexible space and the done button in your toolbar  
    numericInputAccessoryView_.items = [NSArray arrayWithObjects:space,done, nil]; 

// This would return it, if you had this in a separate method. 
    return numericInputAccessoryView_;