0

我有一个布局,其中有两个文本字段,一个用于字母输入,另一个用于数字输入。我为这些设置了不同类型的键盘,例如字母文本字段的默认键盘和数字字段的数字。

我在数字键盘上添加了一个完成按钮。我仍然遇到了键盘问题,即当我先在数字文本上录音时,它显示完成按钮键盘,而我先在字母文本上录音,然后在数字文本字段上录音,然后它不会在数字键盘上添加完成按钮.

我该如何解决?

我已经使用这些代码在数字键盘上添加按钮。

    -(void)viewWillAppear:(BOOL)animated
       {
        // Register for the events

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

        keyboardVisible = NO;
        scroll_view.contentSize=CGSizeMake(320, 400);
       }



        - (void)keyboardDidShow:(NSNotification *)note {
        UIWindow* tempWindow = [[[UIApplication sharedApplication] windows] objectAtIndex:1];
        UIView* keyboard;
        for (keyboard in tempWindow.subviews) {
            if([[keyboard description] hasPrefix:@"<UIKeyboard"] == YES)
                if (numberPadShowing) {
                    [self addButtonToKeyboard];
                    return;                 
                    break;
                } else {
                    for (UIView *v in [keyboard subviews]){
                        if ([v tag]==123)
                            [v removeFromSuperview];
                    }
                }
        }
    }

    -

    (void) keyboardDidHide: (NSNotification *)notif 
   {
        // Is the keyboard already shown
        if (!keyboardVisible) {
            return;
        }
        // Keyboard is no longer visible
        keyboardVisible = NO;
    }

     -(void)doneButton 
   {
    UIScrollView *scrollView =[[UIScrollView alloc] init];
    scrollView=scroll_view;
    [scrollView setContentOffset:svos animated:YES]; 
    [myActiveTextField resignFirstResponder];
   }

        - (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 ([[[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)textFieldDidBeginEditing:(UITextField *)textField
       {
        myActiveTextField=textField;
        if (myActiveTextField==txt_hour_section || myActiveTextField==txt_minute_section || myActiveTextField==txt_transport_time){
            numberPadShowing=TRUE;
            UIScrollView *scrollView =[[UIScrollView alloc] init];
            scrollView=scroll_view;

            svos = scrollView.contentOffset;
            CGPoint pt;
            CGRect rc = [textField bounds];
            rc = [textField convertRect:rc toView:scrollView];
            pt = rc.origin;
            pt.x = 0;
            pt.y -= 60;
            [scrollView setContentOffset:pt animated:YES];           

        }
        else{
            numberPadShowing=FALSE;
        }
    }



     -(BOOL)textFieldShouldReturn:(UITextField *)textField
     {
        UIScrollView *scrollView =[[UIScrollView alloc] init];
        scrollView=scroll_view;
        [scrollView setContentOffset:svos animated:YES]; 
        [textField resignFirstResponder];
       return YES;
    }

提前感谢...

4

2 回答 2

1

我认为您的问题是因为您在点击 alpha 字段后点击了数字字段,并且 alpha 键盘已经在视图中。因此,当您点击数字字段时(虽然 alpha 键盘仍在视图中),它不会触发 'keyboardDidShow' 事件,因为键盘已经从前一个字段显示(尽管是 alpha 键盘)。

尝试将完成按钮逻辑放入数字字段的文本字段“textFieldDidBeginEditing”事件的委托方法中。

于 2012-07-27T06:39:30.710 回答
0

如果我的第一个答案在您的实现中不起作用,那么您可能需要考虑重组并设置 UITextField 的 inputAccessoryView。沿着这些行的代码应该可以解决问题(你可以把它放在你的 viewDidLoad nib 中,还要注意你需要自己实现 doneTapped 方法);

// Create a UIToolbar object and set its style to be black and transparent
    numericInputAccessoryView_ = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
    numericInputAccessoryView_.barStyle = UIBarStyleBlackTranslucent;

// The flexible space item will push the done button to the far right
    UIBarButtonItem *space = [[UIBarButtonItem alloc] 
    initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];

// Make the done button, this is a system item so you don't need to set the title or anything. This will call a method called "doneTapped:(id)sender" when you tap it, you'll need to implement that yourself.
    UIBarButtonItem *done = [[UIBarButtonItem alloc]WithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneTapped:)];

// Stick the flexible space and the done button in your toolbar    
    numericInputAccessoryView_.items = [NSArray arrayWithObjects:space,done, nil];

// This would return it, if you had this in a separate method. 
    return numericInputAccessoryView_;
于 2012-07-27T06:53:01.077 回答