【问题标题】:Remove Done Button From Number Pad upon a New Keyboard Loading加载新键盘时从数字键盘中删除完成按钮
【发布时间】:2010-12-30 05:40:42
【问题描述】:

好的,我会尽力解释这一点。我有一个 iPhone 应用程序,它有一个文本字段,用户只能输入数字。那里没有问题。但是,小键盘上没有完成按钮,所以我不能让它消失。我可以制作一个用户按下以关闭键盘的按钮,但我宁愿有一个完成按钮,因为屏幕照原样“忙碌”。

好吧,经过一番研究,我遇到了this。哇,效果很好。但是,我还有另一个需要默认键盘的文本字段。每当键盘出现时,不管是什么类型的,它上面都有“完成”按钮。

不好。

所以我做了更多的挖掘。通读 cmets,人们提到了一种使用“Editing Did Begin”特性摆脱“完成”按钮的方法,只在必要时调用“完成按钮”代码。我也完成了大部分工作。如果您在数字字段中键入,然后关闭键盘,然后在正常字段中键入,则不会出现完成按钮。

但是,有一个错误,“完成”按钮仍然出现。如果您点击数字字段,然后点击普通字段,键盘永远不会“消失”,因此即使它从小键盘变为普通键盘,按钮仍然存在。我想从视图中删除按钮,但我不知道该怎么做。这是我的代码...

//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 触发“编辑开始”时,它会调用此函数:

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

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

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

谢谢!

【问题讨论】:

    标签: iphone objective-c button keyboard


    【解决方案1】:

    按照这些步骤进行

    一个。首先将 doneButton 设为您的类的实例变量,这将帮助您维护对按钮的引用

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

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

    c。为 keyBoardWillHide 添加通知

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

    d。在 keyBoardWillHide 方法中执行以下操作

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

    这应该可以解决问题。

    【讨论】:

      【解决方案2】:

      您可以使用以下方法:

       //This delegate notify when the user done from the textfield
      - (void) textFieldDidEndEditing:(UITextField *)textField{
          [yourTextField resignFirstResponder];  
          [doneButton removeFromSuperview]; 
          doneButton = nil;
      }
      

      yourTextField 表示键盘上包含完成按钮的文本字段。 前面的方法会从下一个键盘中删除doneButton

      【讨论】:

        猜你喜欢
        • 2012-10-09
        • 2011-05-20
        • 1970-01-01
        • 2023-03-15
        • 1970-01-01
        • 2017-06-24
        • 2012-02-23
        • 2020-02-16
        • 2019-02-14
        相关资源
        最近更新 更多