有没有办法阻止键盘解雇?

时间:2011-05-15 01:57:37

标签: iphone ios ipad uitextfield uikeyboard

我意识到这与大多数帖子相反,但我希望键盘能够保持,即使按下“键盘向下”按钮

具体来说,我有两个UITextField的视图。使用以下委托方法

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    return NO;
}

即使用户按下键盘上的Done按钮或点击屏幕上任何其他位置的按钮,我也可以保持键盘不亮除了键盘右下方那个讨厌的键盘向下按钮。

我正在使用此视图,就像模态视图一样(尽管视图与在UINavigationController中推送的ViewController相关联),因此从用户角度来看,它确实能够始终保持键盘的正常运行。如果有人知道如何实现这一目标,请告诉我!谢谢!

更新仍然没有解决方案!按下Done后会触发textFieldShouldReturn,但按下Dismiss按钮会触发textFieldDidEndEditing。我无法阻止textField结束编辑,或从不消失。不知何故,我真的想要一个检测Dismiss按钮并忽略它的方法。如果你知道一种方法,请赐教!

5 个答案:

答案 0 :(得分:11)

有一种方法可以做到这一点。 因为UIKeyboard子类UIWindow,唯一足以进入UIKeyboard }的方式是另一个UIWindow

- (void)viewDidLoad {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(coverKey) name:UIKeyboardDidShowNotification object:nil];
    [super viewDidLoad];
}

- (void)coverKey {
    CGRect r = [[UIScreen mainScreen] bounds];
    UIWindow *myWindow = [[UIWindow alloc] initWithFrame:CGRectMake(r.size.width - 50 , r.size.height - 50, 50, 50)];
    [myWindow setBackgroundColor:[UIColor clearColor]];
    [super.view addSubview:myWindow];
    [myWindow makeKeyAndVisible];
}

适用于iPhone应用。没试过用iPad。您可能需要调整myWindow的大小。另外,我没有对myWindow进行任何内存管理。所以,也考虑这样做。

答案 1 :(得分:6)

我想我找到了一个很好的解决方案。

添加BOOL作为实例变量,我们称之为shouldBeginCalledBeforeHand

然后实现以下方法:

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    shouldBeginCalledBeforeHand = YES;
    return YES;
}

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField
{
    return shouldBeginCalledBeforeHand;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    shouldBeginCalledBeforeHand = NO;
}

以及

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    return NO;
}

使用返回按钮防止键盘消失。诀窍是,从一个文本字段到另一个文本字段的焦点切换将事先触发textFieldShouldBeginEditing。如果按下关闭键盘按钮,则不会发生这种情况。在文本字段获得焦点后重置该标志。

答案 2 :(得分:3)

旧的不完美的解决方案

我只能想到一个不完美的解决方案。听取通知UIKeyboardDidHideNotification并再次制作文本字段的第一响应者。这会使键盘移开视线并再次返回。您可以通过监听UIKeyboardWillHideNotification来记录哪个文本字段是最后一个第一个响应者,并将重点放在didHide中。

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardDidHide:) 
                                             name:UIKeyboardDidHideNotification
                                           object:nil];

...

- (void)keyboardDidHide:(id)sender
{
    [myTextField becomeFirstResponder];
}

答案 3 :(得分:0)

尝试在键盘关闭按钮顶部添加自定义,以便用户无法选中关闭按钮。我在我的一个应用程序中使用过这种方法。

- (void)addButtonToKeyboard {

    // create custom button
    UIButton *blockButton = [UIButton buttonWithType:UIButtonTypeCustom];
    blockButton.frame = //set the frame here, I don't remember the exact frame
    [blockButton setImage:[UIImage imageNamed:@"block_button.png"] forState:UIControlStateNormal];

    // locate keyboard view
    UIWindow *appWindows = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView *keyboard;
    for (int i=0; i<[appWindows.subviews count]; i++) {
        keyboard = [appWindows.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES && [self.textField isFirstResponder]) {
            [keyboard addSubview:doneButton];

        }
    }

}

答案 4 :(得分:-3)

试试这个......

- (BOOL)textFieldShouldEndEditing:(UITextField *)textField{
   return NO;
}

您可以使用Nick Weaver提到的通知。

相关问题