繁体   English   中英

尝试将完成按钮添加到数字键盘

[英]trying to add done button to Numeric keyboard

我正在尝试向UIKeyboadnumpad添加一个“完成”按钮,但没有成功。 我的代码有什么问题?

键盘没有完成按钮

@implementation DemoViewController

- (void)loadView {
    self.view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];

    textField = [[UITextField alloc] initWithFrame:CGRectMake(10, 200, 300, 26)];
    textField.borderStyle = UITextBorderStyleRoundedRect;
    textField.keyboardType = UIKeyboardTypeNumberPad;
    textField.returnKeyType = UIReturnKeyDone;
    textField.textAlignment = UITextAlignmentLeft;
    textField.text = @"12345";
    [self.view addSubview:textField];

    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(keyboardWillShow:) 
                                                 name:UIKeyboardWillShowNotification 
                                               object:nil];
}

- (void)keyboardWillShow:(NSNotification *)note {  
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [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:0];
    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 ([[[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)doneButton:(id)sender {
    NSLog(@"Input: %@", textField.text);
    [textField resignFirstResponder];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [textField release];
    [super dealloc];
}


@end

另一种方法。 如果屏幕上有其他非数字键盘文本字段,则非常完美。

inputAccessoryView

- (void)viewDidLoad
{
    [super viewDidLoad];

    UIToolbar* numberToolbar = [[UIToolbar alloc]initWithFrame:CGRectMake(0, 0, 320, 50)];
    numberToolbar.barStyle = UIBarStyleBlackTranslucent;
    numberToolbar.items = [NSArray arrayWithObjects:
                         [[UIBarButtonItem alloc]initWithTitle:@"Cancel" style:UIBarButtonItemStyleBordered target:self action:@selector(cancelNumberPad)],
                         [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil],
                         [[UIBarButtonItem alloc]initWithTitle:@"Apply" style:UIBarButtonItemStyleDone target:self action:@selector(doneWithNumberPad)],
                     nil];
    [numberToolbar sizeToFit];
    numberTextField.inputAccessoryView = numberToolbar;
}

-(void)cancelNumberPad{
    [numberTextField resignFirstResponder];
    numberTextField.text = @"";
}

-(void)doneWithNumberPad{
    NSString *numberFromTheKeyboard = numberTextField.text;
    [numberTextField resignFirstResponder];
}

我需要电话垫(带+ *#)而不是数字键盘,我甚至没有角落里的空按钮。

有些教程不完整或者更老。 本教程从IOS 4.3开始工作,我检查了它。 保存两个图像图形,并粘贴代码。 几乎无法改变。 链接在这里。

PS。 我不是以任何方式与本文有任何关联,但发现它是完整的。

http://www.neoos.ch/blog/37-uikeyboardtypenumberpad-and-the-missing-return-key

- (void)keyboardDidShow:(NSNotification *)note写下你的添加按钮的代码

代替

- (void)keyboardWillShow:(NSNotification *)note 

对于此工具, UIKeyboardDidShowNotification通知如下:

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification object:nil];

我认为UIView* keyboard; 没有得到键盘的视图,因为键盘尚未显示,它将显示!

简而言之:截取屏幕截图,剪切整个退格键,水平翻转,在Photoshop中清除其退格符号,并在返回键上覆盖我们想要的文本。 我们选择将其标记为DONE
现在我们有自定义按钮的UIControlStateNormal的图像。
重复整个过程(在截取屏幕截图时使用触摸的退格键)以获取按钮的UIControlStateHighlighted的第二个图像。

结果如下:< 缺少图像 >


现在回到编码:
首先,我们需要知道数字键盘何时在屏幕上向上滑动,以便我们可以在此之前插入自定义按钮。
幸运的是,有一个关于这个目的的通知,并注册它就像:

[[NSNotificationCenter defaultCenter] addObserver:self 
                                         selector:@selector(keyboardWillShow:) 
                                             name:UIKeyboardWillShowNotification 
                                           object:nil];

一旦完成整个事情,不要忘记在适当的地方从通知中心删除观察者

[[NSNotificationCenter defaultCenter] removeObserver:self];

现在我们已经了解它的核心。 我们在keyboardWillShow方法中所要做的就是找到键盘视图并添加我们的按钮。
键盘视图是我们应用程序的第二个UIWindow的一部分,正如其他人已经想到的那样(参见此主题)。
所以我们引用那个窗口(在大多数情况下它将是第二个窗口,所以下面的代码中的objectAtIndex:1很好),遍历它的视图层次结构,直到找到键盘并在左下方添加按钮:

- (void)keyboardWillShow:(NSNotification *)note {  
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [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];
    }
}

Voilà,就是这样!
我们按钮的空白区域从坐标(0,163)开始,并具有尺寸(106,53)。
当然,必须现在编写doneButton方法,但这不再困难。 只需确保在正在编辑的文本字段上调用resignFirstResponder以使键盘向下滑动。

我们“完成了”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM