没有完成按钮的UIKeyboardTypeNumberPad

时间:2011-01-28 02:44:59

标签: iphone cocoa-touch ios uikit uikeyboard

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

4 个答案:

答案 0 :(得分:13)

如果我没有错,那么您想询问如何为UIKeyboardTypeNumberPad的键盘添加自定义“完成”按钮。在这种情况下,这可能会有所帮助。在in.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没有完成按钮。enter image description here

答案 1 :(得分: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];
        }
    }

}

答案 2 :(得分:1)

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];
 }

做了一个款待。你是个明星。谢谢,尼古拉; - )

答案 3 :(得分:1)

如果有人在尝试在同一个应用程序中加载其他类型的键盘时出现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];
}

}