2013-05-20 74 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