2

我是新手,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。

谁能帮助我?

提前致谢。

4

3 回答 3

2

您可以通过检查以下链接来做到这一点。

参考1

参考文献 2

但请不要这样做。

考虑一下:

  • 如果数字键盘的布局发生变化怎么办?
  • 如果按键的颜色发生变化怎么办?
  • 如果键盘的实现发生变化,使其不再是索引 1 处的窗口怎么办?
  • 如果键盘和外围主机的实现发生变化,以至于内省描述中断怎么办?
  • 如果您在键盘布局完全不同的 iPad 上运行它怎么办?
于 2013-05-20T09:20:11.687 回答
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];        
    }   
}
于 2013-05-20T09:11:56.483 回答
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];
}
于 2013-05-20T09:46:21.333 回答