在数字键盘上添加完成按钮

发布于 2024-11-03 19:53:57 字数 4211 浏览 0 评论 0原文

我有一份注册表,其中有五个文本字段,其中一个文本字段是电话号码文本字段,我们使用键盘作为该 uikeyboardtypenumberpad,但 numberpad 键盘上没有完成按钮。我正在添加“完成”按钮,但为所有文本字段添加了按钮,但我只想为电话号码字段添加按钮。

我的代码是:

- (void)viewDidLoad
{
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;
//    UIBarButtonItem *rightbarButton = [[UIBarButtonItem alloc]initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(AboutButtonAction)];
//    self.navigationItem.rightBarButtonItem = rightbarButton;
    [super viewDidLoad];
    NSLog(@"Events Id is : %@", particularEventId);
    phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(56, 223, 220, 31)];
    phoneNumber.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    phoneNumber.returnKeyType = UIReturnKeyDone;
    phoneNumber.textAlignment = UITextAlignmentLeft;

    [self.view addSubview:phoneNumber];
    // add observer for the respective notifications (depending on the os version)
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification 
                                                   object:nil];     
    } else {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];
    }
}

- (void)addButtonToKeyboard {
    // create custom button

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [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 found, add the button
        if(keyboard == phoneNumber)
        {
        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)keyboardWillShow:(NSNotification *)note {
    // if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
        [self addButtonToKeyboard];
    }
}

- (void)keyboardDidShow:(NSNotification *)note {
    // if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [self addButtonToKeyboard];
    }
}


- (void)doneButton:(id)sender {
    NSLog(@"doneButton");
    NSLog(@"Input: %@", phoneNumber.text);
    [phoneNumber resignFirstResponder];
}

I am having one registration form which are having five textfield in which one textfield is phonenumber textfield and we take keyboard for that uikeyboardtypenumberpad, but no done button on numberpadkeyboard. I am adding done button but button is added for all textfields but I want only for phonenumber field.

My code is:

- (void)viewDidLoad
{
    self.navigationController.navigationBarHidden = NO;
    self.navigationController.navigationBar.tintColor = [UIColor blackColor];

    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc]initWithTitle:@"Back" style:UIBarButtonItemStyleBordered target:self action:@selector(backButtonAction)];
    self.navigationItem.leftBarButtonItem = leftButton;
//    UIBarButtonItem *rightbarButton = [[UIBarButtonItem alloc]initWithTitle:@"About" style:UIBarButtonItemStyleBordered target:self action:@selector(AboutButtonAction)];
//    self.navigationItem.rightBarButtonItem = rightbarButton;
    [super viewDidLoad];
    NSLog(@"Events Id is : %@", particularEventId);
    phoneNumber = [[UITextField alloc] initWithFrame:CGRectMake(56, 223, 220, 31)];
    phoneNumber.borderStyle = UITextBorderStyleRoundedRect;
    phoneNumber.keyboardType = UIKeyboardTypeNumberPad;
    phoneNumber.returnKeyType = UIReturnKeyDone;
    phoneNumber.textAlignment = UITextAlignmentLeft;

    [self.view addSubview:phoneNumber];
    // add observer for the respective notifications (depending on the os version)
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardDidShow:) 
                                                     name:UIKeyboardDidShowNotification 
                                                   object:nil];     
    } else {
        [[NSNotificationCenter defaultCenter] addObserver:self 
                                                 selector:@selector(keyboardWillShow:) 
                                                     name:UIKeyboardWillShowNotification 
                                                   object:nil];
    }
}

- (void)addButtonToKeyboard {
    // create custom button

    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.0) {
        [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
        [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    } else {        
        [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 found, add the button
        if(keyboard == phoneNumber)
        {
        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)keyboardWillShow:(NSNotification *)note {
    // if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] < 3.2) {
        [self addButtonToKeyboard];
    }
}

- (void)keyboardDidShow:(NSNotification *)note {
    // if clause is just an additional precaution, you could also dismiss it
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 3.2) {
        [self addButtonToKeyboard];
    }
}


- (void)doneButton:(id)sender {
    NSLog(@"doneButton");
    NSLog(@"Input: %@", phoneNumber.text);
    [phoneNumber resignFirstResponder];
}

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

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

发布评论

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

评论(1

九歌凝 2024-11-10 19:53:57

我在我的项目中使用了这段代码,我认为它没有优化,但效果很好。

.h.m

BOOL firstTime;
BOOL add;
BOOL keyboardOpened;

- (void)viewDidLoad
{
    [super viewDidLoad];

    firstTime = TRUE;
    add = TRUE;

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

- (void)addButtonToKeyboard {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    doneButton.tag = 3;
    [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 found, add the button
        if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES && add) [keyboard addSubview:doneButton];
    }
}

- (void)removeButtonFromKeyboard
{
    // 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 found, remove the button
        if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) [[keyboard viewWithTag:3] removeFromSuperview];
    }
}

- (void)doneButton:(id)sender {
    [firstResponder resignFirstResponder];
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
    {
        [self removeButtonFromKeyboard];
        firstTime = TRUE;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    firstResponder = textField;

    for (int i = 0; i < [[[profile operationObject] keys] count]; i++) 
    {
        if ([[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] viewWithTag:1] isEqual:textField]) 
        {
            editingPath = [NSIndexPath indexPathForRow:i inSection:0];
            break;
        }
    }

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
    {
        if(!firstTime)
        {
            if(textField.keyboardType == UIKeyboardTypeNumberPad)
            {
                add = TRUE;
                [self addButtonToKeyboard];
            }
            else
            {
                add = FALSE;
            }
        }
        else
        {
            firstTime = FALSE;
            if(textField.keyboardType != UIKeyboardTypeNumberPad)
            {
                add = FALSE;
            }
        }
        keyboardOpened = TRUE;
    }
}

- (void)keyboardDidShow:(id)sender
{
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    {
        NSLog(@"%@",[[UIDevice currentDevice] model]);
        [self addButtonToKeyboard];
        keyboardOpened = TRUE;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [[[profile operationObject] values] replaceObjectAtIndex:[editingPath row] withObject:[textField text]];
    firstResponder = nil;

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
    {
        [self removeButtonFromKeyboard];
        keyboardOpened = FALSE;
    }
}

I used this code for my project, I think it isn't optimized but it works very well.

.h

BOOL firstTime;
BOOL add;
BOOL keyboardOpened;

.m

- (void)viewDidLoad
{
    [super viewDidLoad];

    firstTime = TRUE;
    add = TRUE;

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

- (void)addButtonToKeyboard {
    // create custom button
    UIButton *doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;
    [doneButton setImage:[UIImage imageNamed:@"DoneUp3.png"] forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"DoneDown3.png"] forState:UIControlStateHighlighted];
    doneButton.tag = 3;
    [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 found, add the button
        if ([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES && add) [keyboard addSubview:doneButton];
    }
}

- (void)removeButtonFromKeyboard
{
    // 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 found, remove the button
        if([[keyboard description] hasPrefix:@"<UIPeripheralHost"] == YES) [[keyboard viewWithTag:3] removeFromSuperview];
    }
}

- (void)doneButton:(id)sender {
    [firstResponder resignFirstResponder];
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
    {
        [self removeButtonFromKeyboard];
        firstTime = TRUE;
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)theTextField {
    [theTextField resignFirstResponder];
    return YES;
}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    firstResponder = textField;

    for (int i = 0; i < [[[profile operationObject] keys] count]; i++) 
    {
        if ([[[self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:i inSection:0]] viewWithTag:1] isEqual:textField]) 
        {
            editingPath = [NSIndexPath indexPathForRow:i inSection:0];
            break;
        }
    }

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
    {
        if(!firstTime)
        {
            if(textField.keyboardType == UIKeyboardTypeNumberPad)
            {
                add = TRUE;
                [self addButtonToKeyboard];
            }
            else
            {
                add = FALSE;
            }
        }
        else
        {
            firstTime = FALSE;
            if(textField.keyboardType != UIKeyboardTypeNumberPad)
            {
                add = FALSE;
            }
        }
        keyboardOpened = TRUE;
    }
}

- (void)keyboardDidShow:(id)sender
{
    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"]) 
    {
        NSLog(@"%@",[[UIDevice currentDevice] model]);
        [self addButtonToKeyboard];
        keyboardOpened = TRUE;
    }
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [[[profile operationObject] values] replaceObjectAtIndex:[editingPath row] withObject:[textField text]];
    firstResponder = nil;

    if (![[[UIDevice currentDevice] model] isEqualToString:@"iPad"] && ![[[UIDevice currentDevice] model] isEqualToString:@"iPad Simulator"])
    {
        [self removeButtonFromKeyboard];
        keyboardOpened = FALSE;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文