0

1つはアルファベット入力用、もう1つは数値入力用の2つのテキストフィールドを持つレイアウトがあります。これらには、アルファベットのテキスト フィールドのデフォルトや数値フィールドの数字など、さまざまなタイプのキーボードを設定しました。

テンキーに完了ボタンを追加しました。それでも、キーボードで問題が発生しています。最初に数字テキスト フィールドをテープで貼り付けた後、完了ボタン キーボードが表示され、最初にアルファベット テキスト フィールドをテープで貼り付けてから数値テキスト フィールドをテープで貼り付けた後、数字パッドに完了ボタンが追加されません。 .

どうすれば解決できますか?

これらのコードを使用して、テンキーにボタンを追加しました。

    -(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

あなたの問題は、アルファフィールドをタップした後に数値フィールドをタップしていて、アルファキーボードがすでに表示されているためだと思います。したがって、数値フィールドをタップしても (アルファ キーボードがまだ表示されているときに)、キーボードは前のフィールドから既に表示されているため (アルファ キーボードではありますが)、「keyboardDidShow」イベントは発生しません。

数値フィールドの textfield '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 に答える