【问题标题】:trying to add done button to Numeric keyboard尝试将完成按钮添加到数字键盘
【发布时间】:2012-02-23 04:31:04
【问题描述】:

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

【问题讨论】:

  • 您的代码标签似乎不起作用。
  • 您的代码示例显示不正确,但它已由一位好心的志愿者修复,没关系。 :)
  • 我在互联网上找到了这段代码并尝试修复它并使其无法成功,如果您有任何线索请帮助我,这是什么问题
  • 在您尝试执行此操作之前,请注意此答案stackoverflow.com/a/8246080/41116 中反对这种自定义的原因

标签: iphone objective-c ios


【解决方案1】:

简而言之:截取屏幕截图,剪下整个退格键,水平翻转,在 Photoshop 中清除退格符号,然后在 return 键上覆盖我们想要的文本。我们选择将其标记为 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];
    }
}

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

我们“完成”了。

【讨论】:

    【解决方案2】:

    另一种解决方案。如果屏幕上有其他非数字键盘文本字段,那就完美了。

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

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

    【讨论】:

    • 如果我有几个数字字段,我想知道谁是发件人?
    • @Dejel,让每个数字字段成为一个实例变量。
    【解决方案3】:

    有些教程不完整或较旧。本教程从 IOS 4.3 开始工作,我检查了它。保存两个图像图形,并粘贴代码。几乎没有什么可以改变的。这是链接。

    ps。我与这篇文章没有任何关联,但发现它是完整的。

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

    【讨论】:

      【解决方案4】:

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

      而不是

      - (void)keyboardWillShow:(NSNotification *)note 
      

      为此实现UIKeyboardDidShowNotification 通知,例如:

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

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

      【讨论】:

        猜你喜欢
        • 2011-05-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-03-05
        • 2013-09-21
        • 2020-03-16
        • 2013-12-10
        • 1970-01-01
        相关资源
        最近更新 更多