检测哪个uitextfield称为uikeyboard

时间:2011-10-21 11:25:12

标签: objective-c

经过很多麻烦后,我可以在数字键盘上添加一个RETURN按钮。

我想在将其他数据引入其他uitextfields时将其删除,因为当我使用键盘时,每次出现该按钮时,它都是键盘的一部分。

我在KeyBoardDidShow上添加事件处理程序,否则它不起作用,

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

然后在KeyboarWillShow

- (void)keyboardWillShow:(NSNotification *)note {  

    // create custom button

    NSLog(@"El note es: %@", note);

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"done.jpg"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"done_pressed.jpg"] forState:UIControlStateHighlighted];
    [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];

    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 (UIView *possibleKeyboard in [keyboardWindow subviews]) {

        // iOS 4 sticks the UIKeyboard inside a UIPeripheralHostView.
        if ([[possibleKeyboard description] hasPrefix:@"<UIPeripheralHostView"]) {
            possibleKeyboard = [[possibleKeyboard subviews] objectAtIndex:0];
        }

        if ([[possibleKeyboard description] hasPrefix:@"<UIKeyboard"]) {
            foundKeyboard = possibleKeyboard;
            break;
        }
    }

    if (foundKeyboard) {
        // Add the button to foundKeyboard.
        [foundKeyboard addSubview:doneButton];

    }
}

事情就是这总是创造了按钮!明显!我想在某些uitextfields中创建按钮,而不是全部,但是如何做呢?

方法 - (BOOL)textFieldShouldBeginEditing:第一次没有执行,只剩下其余的,为什么?不知道,但如果我能够知道哪个uitextfield正在调用键盘,我的头发会再次生长而且我会变得更苗条,任何人都可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

如果每个文本字段都设置了正确的委托(检查您的XIB),

textFieldShouldBeginEditing:只会持续触发。

然后您可以在文本字段中设置标记值。您可以为标记为1的字段显示“返回”按钮,而对于标记为0的字段,则不显示“返回”按钮。

答案 1 :(得分:0)

您可以尝试计算键盘内的子视图数量,以了解其拥有的密钥数量。这是一个糟糕的黑客,但你已经找到了一个糟糕的黑客键盘:-)

另一个建议是:

 if (foundKeyboard) {
    UIView *doneButton = [foundKeyboard viewWithTag:BUTTON_TAG];

    if(numPad){
        if(buttonView==nil){
            //create doneButton
            [foundKeyboard addSubview:doneButton];          
        }
    }else{  
        [doneButton removeFromSuperview];
    }
}