没有完成按钮的 UIKeyboardTypeNumberPad

发布于 2024-10-14 09:58:39 字数 60 浏览 2 评论 0原文

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

How can we implement UIKeyboardTypeNumberPad so that it will have a 'done' button? By default it does not have one.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

眼中杀气 2024-10-21 09:58:39

如果我没记错的话,那么您想询问如何为 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 没有“完成”按钮。在此处输入图像描述

If I am not wrong then You want to ask as to how to add a custom "Done" button to keyboard for UIKeyboardTypeNumberPad . In that case this might be helpful. Declare a UIButton *doneButton in.h and add the following code to .m file

- (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
}

I am using the same in my application and the button's frame are thus adjusted, You can thus call [self addButtonToKeyboard ] whenever you need to show up the done button over the keyboard. UIKeyboardTypeNumberPad has no Done button otherwise.enter image description here

浪荡不羁 2024-10-21 09:58:39
 [[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];
        }
    }

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

}
孤蝉 2024-10-21 09:58:39

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

工作得很好。你是一个明星。谢谢,尼古拉;-)

iOS 5 issue resolved. Thanks a million.

I had a problem with displaying the Done button.

Replaced:

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

which did not display the done button in iOS 5...

With:

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

Worked a treat. You're a star. Thanks, Nicola ;-)

好倦 2024-10-21 09:58:39

如果有人遇到这样的问题:当您尝试在同一应用程序中加载其他类型的键盘时,“完成”按钮不断弹出,我知道很多应用程序都存在此问题。
不管怎样,我是这样解决这个问题的:
在您的 ViewController(您添加“完成”按钮的同一视图控制器)中添加此代码(按原样),它应该可以解决“完成”按钮不断重新出现的问题。
希望它对一些人有帮助。

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

}

If anybody had a problem that the DONE button keeps popping up when you try to load other types of keyboards in the same app, I know a lot of apps out there have this issue.
Anyway, here is how I solved this:
In your ViewController ( the same viewcontroller that you added DONE button ) add this code ( as is ) and it should solve your problems with DONE button keep reappearing.
Hope it helps to some folks.

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

}

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文