我想为不同的textField提供不同的键盘用于注册表单

时间:2014-07-30 09:27:39

标签: xcode

我想在点击tfZip textField时用完成按钮显示用户数字键盘键盘,否则简单。但我的问题是每个键盘上都显示完成按钮。请帮助,我搜索3-4小时但不是得到正确答案。

显然,我有textField,其中一个是Zip.Now我为不同的textField设置了不同的键盘。“当我第一次点击textField接受Zip textField它的确定(键盘)然后点击Zip textField我得到no键盘上的完成按钮。“

-(void)textFieldDidBeginEditing:(UITextField *)textField
{
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(keyboardWillShow:)
                                                 name:UIKeyboardWillShowNotification
                                               object:nil];

    if(!(textField == tfZip))
    {
        doneButton.hidden=YES;
    }
}

-(void)textFieldDidEndEditing:(UITextField *)textField {

}

- (void)keyboardWillShow:(NSNotification *)note {
    // create custom button

    doneButton = [UIButton buttonWithType:UIButtonTypeCustom];
    doneButton.frame = CGRectMake(0, 163, 106, 53);
    doneButton.adjustsImageWhenHighlighted = NO;

    [doneButton setImage:[UIImage imageNamed:@"Done.png"]
                forState:UIControlStateNormal];
    [doneButton setImage:[UIImage imageNamed:@"Done_select.png"]
                forState:UIControlStateHighlighted];

    [doneButton addTarget:self
                   action:@selector(doneButton:)
         forControlEvents:UIControlEventTouchUpInside];

    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"7.0")) {
        dispatch_async(dispatch_get_main_queue(), ^{
            UIView *keyboardView = [[[[[UIApplication sharedApplication] windows] lastObject] subviews] firstObject];
            [doneButton setFrame:CGRectMake(0, keyboardView.frame.size.height - 53, 106, 53)];
            [keyboardView addSubview:doneButton];
            [keyboardView bringSubviewToFront:doneButton];

            [UIView animateWithDuration:[[note.userInfo objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue]-.02
                                  delay:.0
                                options:[[note.userInfo objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue]
                             animations:^{
                                 self.view.frame = CGRectOffset(self.view.frame, 0, 0);
                             } completion:nil];
        });
    } else {
        // locate keyboard view
        dispatch_async(dispatch_get_main_queue(), ^{
            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];
            }
        });
    }
}

- (void)doneButton:(id)sender
{
    [tfZip resignFirstResponder];
}

-(void)textFieldDidEndEditing:(UITextField *)textField
{
    if(!(textField==tfZip))
    {
        doneButton.hidden=NO;
    }
}

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

2 个答案:

答案 0 :(得分:0)

将textfield delegate方法用于仅显示tfZip的完成按钮

-(BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{

     if(textField==tfZip)
     {
          doneButton.hidden=NO;
     }
     else
    {
          doneButton.hidden=YES;
     }
   return YES;
}

答案 1 :(得分:0)

我猜你需要检查一下 - (void)keyboardWillShow:(NSNotification *)note;在将完成按钮添加为子视图之前。