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

时间:2012-01-31 12:51:25

标签: iphone objective-c ios

我正在尝试向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

4 个答案:

答案 0 :(得分:27)

另一种解决方案。如果屏幕上还有其他非数字键盘文本字段,则表示完美。

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

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

答案 1 :(得分:1)

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

而不是

- (void)keyboardWillShow:(NSNotification *)note 

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

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

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

答案 2 :(得分:1)

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

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

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

答案 3 :(得分:0)

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

结果如下:&lt; 缺少图片&gt;


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

[[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以使键盘向下滑动。

我们“完成了”。