2013-05-20 73 views
2

我是新的IOS &我正在使用IOS 6。我的代碼中有多個文本字段,其中一個使用數字鍵盤鍵盤,我想將自定義按鈕添加到它。添加自定義按鈕到數字鍵盤的鍵盤ios 6

我使用這個代碼:

UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 163, 106, 53); 
    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]; 
    UIView* keyboard; 
    for(int i=0; i<[tempWindow.subviews count]; i++) { 
     keyboard = [tempWindow.subviews objectAtIndex:i]; 
     // keyboard view found; add the custom button to it 
     if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == TRUE) 
      [keyboard addSubview:doneButton]; 
     } 

選擇是所謂的,但問題是,[[tempwindow.subviews]count]爲0

誰能幫助我?

在此先感謝。

+0

我會建議使用[InputAccessoryView(http://stackoverflow.com/a/11211721/593709) –

回答

0

我已經使用下面的代碼。覈實。

- (void)textFieldDidBeginEditing:(UITextField *)textField { 
    if(textField==txt1) 
    { 

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

    } 
    else 
    { 

     [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 
    } 



} 
- (void) keyboardDidShowOrHide : (id) sender { 
    // create custom button 
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom]; 
    doneButton.frame = CGRectMake(0, 427, 106, 53); 
    doneButton.adjustsImageWhenHighlighted = NO; 
    [doneButton setBackgroundImage:[UIImage imageNamed:@"doneup.png"] forState:UIControlStateNormal]; 
    [doneButton setBackgroundImage:[UIImage imageNamed:@"donedown.png"] forState:UIControlStateHighlighted]; 
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside]; 
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 
    [tempWindow addSubview:doneButton]; 


} 
-(void)doneButton:(id)sender 
{ 
    UIButton *btntmp=sender; 
    [btntmp removeFromSuperview]; 
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil]; 
    //[self setViewMovedUp:NO]; 
    [txt1 resignFirstResponder]; 
} 
1

對於我來說,我使用下面的代碼片段添加按鈕,鍵盤:

- (void)addHideKeyboardButtonToKeyboard { 
    // Locate non-UIWindow. 
    UIWindow *keyboardWindow = nil; 
    for (UIWindow *testWindow in [[UIApplication sharedApplication] windows]) { 
     if (![[testWindow class] isEqual:[UIWindow class]]) { 
      keyboardWindow = testWindow; 
      break; 
     } 
    } 
    if (!keyboardWindow) return; 

    // Locate UIKeyboard. 
    UIView *foundKeyboard = nil; 
    for (__strong UIView *possibleKeyboard in [keyboardWindow subviews]) { 
     // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView. 
     if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) { 
      for (__strong UIView *anotherPossibleKeyboard in [possibleKeyboard subviews]) {  
       if ([[anotherPossibleKeyboard description] hasPrefix:@"<UIKeyboard"]) { 
        foundKeyboard = possibleKeyboard; 
        break; 
       } 
      } 
     } 
    } 

    if (foundKeyboard) { 
     [foundKeyboard addSubview:self.keyboardDoneButton];   
    } 
} 
+0

我用了你的代碼,但它增加了按鈕,然後它disappeard –

+0

可以嘗試建立完成按鈕作爲一個強大的財產,以便它將被保留? –

+0

是的,我作爲一個強大的財產,我仍然有這個問題 –

2

您可以通過檢查以下鏈接做到這一點。

Reference 1

Reference 2

但請不要這樣做。

考慮一下:

  • 如果有什麼的數字鍵盤的佈局改變?
  • 如果按鍵的顏色改變了怎麼辦?
  • 如果鍵盤的執行發生變化,那麼在索引1處窗口的長度不再增加 會怎樣?
  • 如果鍵盤和外圍設備主機的實現更改 以使內容描述中斷?
  • 如果你在iPad上運行這個鍵盤有一個完全 不同的佈局呢?
+0

問題是for循環的計數是0,我的意思是'UIWindow * tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];'[[tempWindow.subViews] count]'爲零 –

+0

它對我很好。 – Shardul