【问题标题】:UIKeyboardTypeNumberPad without a done button没有完成按钮的 UIKeyboardTypeNumberPad
【发布时间】:2011-06-16 23:56:43
【问题描述】:

我们如何实现 UIKeyboardTypeNumberPad 以便它有一个“完成”按钮?默认情况下它没有。

【问题讨论】:

  • 实现键盘是什么意思,你想隐藏键盘或其他任何东西。
  • 不...只是键盘上没有完成按钮
  • 亲爱的数字键盘没有任何完成按钮。所以您的问题无效。

标签: iphone cocoa-touch ios uikit uikeyboard


【解决方案1】:

如果我没有错,那么您想询问如何为 UIKeyboardTypeNumberPad 添加自定义“完成”按钮到键盘。在这种情况下,这可能会有所帮助。在.h 中声明一个 UIButton *doneButton 并将以下代码添加到 .m 文件中

- (void)addButtonToKeyboard {
    // create custom button
    if (doneButton == nil) {
        doneButton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
    }
    else {
        [doneButton setHidden:NO];
    }

    [doneButton addTarget:self action:@selector(doneButtonClicked:) forControlEvents:UIControlEventTouchUpInside];
    // locate keyboard view
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard = nil;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];
        // keyboard found, add the button
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        } else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
    }
}

- (void)doneButtonClicked:(id)Sender {
//Write your code whatever you want to do on done button tap
//Removing keyboard or something else
}

我在我的应用程序中使用了相同的按钮,因此调整了按钮的框架,因此您可以在需要在键盘上显示完成按钮时调用 [self addButtonToKeyboard ]。否则 UIKeyboardTypeNumberPad 没有完成按钮。

【讨论】:

  • UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1]; 这行有问题我得到没有子视图的空数组
  • 你从哪里得到一个空白数组。如果屏幕上出现键盘,您肯定会在数组中获得至少一项,即键盘。
  • 我已经在keyboardwilllshow和keyboarddidshow中添加了对这个函数的调用,但我仍然得到空数组
  • 当您将文本字段或 Textview 作为第一响应者时,请尝试在其他时间拨打电话。
【解决方案2】:
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardDidShowNotification object:nil];


//Call this method 

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


   UIButton *doneButton  = [[UIButton alloc] initWithFrame:CGRectMake(0, 163, 106, 53)];
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"Done.png"] forState:UIControlStateNormal];
   [doneButton addTarget:self action:@selector(doneButton:) forControlEvents:UIControlEventTouchUpInside];
    UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
    UIView* keyboard;
    for(int i=0; i<[tempWindow.subviews count]; i++) {
        keyboard = [tempWindow.subviews objectAtIndex:i];

        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
            if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
                [keyboard addSubview:doneButton];
        }
        else {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                [keyboard addSubview:doneButton];
        }
    }

}

【讨论】:

    【解决方案3】:

    iOS 5 问题已解决。谢谢一百万。

    我在显示“完成”按钮时遇到问题。

    替换:

     if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
         [keyboard addSubview:doneButton];
    

    在 iOS 5 中没有显示完成按钮...

    与:

     if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES)
            [keyboard addSubview:doneButton];
     } else {
        if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
            [keyboard addSubview:doneButton];
     }
    

    辛苦了。你是个明星。谢谢,尼古拉 ;-)

    【讨论】:

      【解决方案4】:

      如果有人在尝试在同一应用中加载其他类型的键盘时遇到 DONE 按钮不断弹出的问题,我知道很多应用都存在此问题。 无论如何,这是我解决这个问题的方法: 在您的 ViewController (您添加 DONE 按钮的同一 viewcontroller )中添加此代码(按原样),它应该解决您的问题,DONE 按钮不断重新出现。 希望对大家有所帮助。

      - (void)viewWillDisappear:(BOOL)animated {
      [super viewWillDisappear:animated];
      if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
          [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidShowNotification object:nil];   
          [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardDidHideNotification object:nil];
      
      } else {
          [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillShowNotification object:nil];  
          [[NSNotificationCenter defaultCenter] removeObserver:self name:UIKeyboardWillHideNotification object:nil];
      }
      

      }

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-01-18
        • 2013-11-22
        • 1970-01-01
        • 2019-02-14
        • 1970-01-01
        相关资源
        最近更新 更多