添加自定义按钮到数字键盘键盘ios 6

时间:2013-05-20 08:59:45

标签: objective-c ios6

我是IOS&我正在使用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。

有人可以帮助我吗?

提前致谢。

3 个答案:

答案 0 :(得分:2)

您可以通过查看以下链接来完成此操作。

Reference 1

Reference 2

但请不要这样做。

考虑一下:

  • 如果数字键盘的布局发生变化怎么办?
  • 如果键的颜色发生变化怎么办?
  • 如果键盘的实现发生变化,那就不行了 索引1的窗口越长?
  • 如果键盘和外围设备主机的实现发生了变化 内省的描述会中断吗?
  • 如果你在iPad上运行这个键盘,那么键盘就完​​全可以了 不同的布局?

答案 1 :(得分: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];        
    }   
}

答案 2 :(得分: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];
}