加载新键盘后,从数字键盘上删除完成的按钮 [英] Remove Done Button From Number Pad upon a New Keyboard Loading

查看:110
本文介绍了加载新键盘后,从数字键盘上删除完成的按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,我会尽力解释这一点.我有一个iPhone应用程序,它有一个文本字段,用户只能输入数字.那里没问题.但是,数字键盘上没有完成按钮,因此我无法使其消失.我可以创建一个用户可以按下以关闭键盘的按钮,但是我宁愿有一个完成的按钮,因为屏幕原样忙"着.

Alright, I'll try to explain this the best I can. I have an iPhone app and it has a text field which the user can only input Numbers. Not a problem there. However, there is no done button on the numpad, so I can't make it disappear. I could make a button that the user presses to dismiss the keyboard, but I'd rather have a done button because the screen is "busy" as is.

嗯,经过一番研究,我遇到了

Well, after some research, I came across this. Wow, that worked great. However, I also have another text field which requires the default keyboard. And whenever the keyboard comes up, regardless of the type, it has the "Done" button on it.

不好.

因此,我还要进行更多挖掘.仔细阅读注释,人们提到了一种使用编辑完成开始"特征摆脱完成"按钮的方法,仅在必要时调用完成按钮"代码.我也大部分都完成了.如果您在数字字段中键入,然后关闭键盘,然后在普通字段中键入,则完成按钮不会出现.

So I do some more digging. Read through the comments, and people mentioned a way to get rid of the Done button using the "Editing Did Begin" trait, to only call the "Done Button" code when necessary. I also got that mostly done. If you type in the number field, and then dismiss the keyboard, and then type in the normal field, the done button does not appear.

但是,有一个错误,完成"按钮仍然出现.如果您单击数字字段,然后单击普通字段,则键盘永远不会消失",因此,即使从数字键盘更改为普通键盘,该按钮也仍然存在.我想从视图中删除按钮,但是不确定如何执行此操作.这是我的代码...

However, there is one bug, where the "Done" button still appears. If you tap the Numeric Field, and then tap the Normal field, the keyboard never "disappears", so even though it changes from a numpad to a normal keyboard, the button still is there. I want to remove the buttom from the view, but I'm not sure how to go about this. This is the code I have...

//Done button for numpad
- (void)keyboardWillShow:(NSNotification *)note {
    if (showDoneButton == YES)
    {   


    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] hasPrefix:@"3"]) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [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:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
    }
        showDoneButton = NO; //This tells done button code to run or not
    }
}

- (void)doneButton:(id)sender {
    NSLog(@"Input: %@", playerOneLifeLabel.text);
    NSLog(@"Input: %@", playerTwoLifeLabel.text);
    [playerOneLifeLabel resignFirstResponder];
    [playerTwoLifeLabel resignFirstResponder];
}

当NumPadfield触发编辑已开始"时,它将调用此函数:

When the NumPadfield triggers "editing did begin" it calls this function:

- (IBAction)needNumberPad:(id)sender{
    showDoneButton = YES;
}

然后确保显示完成按钮.完成按钮代码完成后,变量(如上所示)被设置回NO,因此,如果您点击默认文本字段,该变量将不会再次显示.布尔变量默认设置为NO.

Which then makes sure the done button is shown. When the done button code is done, the variable (as seen above) is set back to NO, so it doesn't show up again if you tap the default text field. The Boolean variable is set to NO as default.

需要发生的是,如果您立即从编辑numPad字段跳转到默认字段,则完成按钮将消失.我可以将代码放在编辑结束"时调用的函数中,但是我不知道该放在哪里.帮助将不胜感激,我已经走了这么远!

What needs to happen is if you jump immediately from editing the numPad field to the default field, the done button disappears. I can put the code in the function that is called when "Editing did end" but I don't know what to put there. Help would be appreciated, I've gotten this far!

谢谢!

推荐答案

执行以下步骤

a.首先将doneButton设为您的类的实例变量,这将有助于您维护对按钮的引用

a. First make the doneButton an instance varible of your class, this will help you maintain the reference to the button

b.将此代码添加到keyboardWillShow:(NSNotification *)note方法的开头

b. Add this code at the beginning of your keyboardWillShow:(NSNotification *)note method

if(!showDoneButton ){
      if(doneButton){
[doneButton removeFromSuperview];
doneButton = nil;
      }
      return;
 }

c.为keyBoardWillHide添加通知

c. add notification for keyBoardWillHide

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillHide:) 
                                             name:UIKeyboardWillHideNotification 
                                           object:nil];

d.在keyBoardWillHide方法中执行以下

d. in keyBoardWillHide method do the following

- (void)keyboardWillHide:(NSNotification *)note 
{
if(doneButton)
{
    [doneButton removeFromSuperview];
    doneButton = nil;
}
}

这应该可以解决问题.

这篇关于加载新键盘后,从数字键盘上删除完成的按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆