2

コンバーターアプリを作成しています。メイン画面には数値を入力するためのテキストフィールドがあり、テキストフィールドの下にピッカービューがあり、ユーザーは変換パラメーター(たとえば、kgからg)を選択できます。

次の方法で、ユーザーが背景をクリックしたときにキーボードを非表示にできます

 (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
 [self.enterInput resignFirstResponder];

しかし、ピッカービューに触れると、キーボードが隠れていません。

私の質問は、ユーザーがピッカービューに触れたときにキーボードを閉じる方法です。

4

3 に答える 3

2

解決策を得た

1)最初に非表示のroundRectボットンを作成し、タイプをカスタムに変更します(ピッカーのサイズに合わせます)。

2)タッチアップインサイドアクションを作成する

 - (IBAction)hiddenButtonToHideKeyboard:(id)sender {
    [self.enterInput resignFirstResponder];
}

3)キーボード表示通知を作成します

[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardAppear:) name:UIKeyboardWillShowNotification object:nil];

4)キーボードの消失通知を作成します

 [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(onKeyboardHide:) name:UIKeyboardWillHideNotification object:nil];

5)キーボードが表示されたときにボタンが表示されるようにします

 -(void)onKeyboardAppear:(NSNotification *)notification
{
    hiddenButtonToHideKeyboard.hidden=NO;
}

6)キーボードが消えたらボタンを非表示にします

-(void)onKeyboardHide:(NSNotification *)notification
{
    hiddenButtonToHideKeyboard.hidden=YES;
}

5)完了

私はそれが完璧な解決策だとは思いませんが、それは私にとってはうまくいきます:)

于 2012-06-11T10:26:13.500 に答える
1

私はこれを自分のコードで使用しています。レスポンダーチェーンを登り、レスポンダーを再割り当てします。これをpickerViewを表示するメソッドに入れました。これまでのところ、予期しない問題はありません。キーボードが表示されている場合は機能しているようで、キーボードが表示されていない場合はクラッシュしないようです。

    [[UIApplication sharedApplication] sendAction:@selector(resignFirstResponder) to:nil from:nil forEvent:nil];
于 2012-10-26T19:56:38.757 に答える
0

どうぞ。UIPickerViewsは、ネストされたUIViewのかなり複雑なシステムであるため、touchesBegan:withEvent:メソッドから応答が得られませんでした。できることは、次のようにUIPickerViewサブクラスを作成することです。

//
//  MyPickerView.h
//

#import <UIKit/UIKit.h>

// Protocol Definition that extends UIPickerViewDelegate and adds a method to indicate a touch
@protocol MyPickerViewDelegate <UIPickerViewDelegate>

// This is the method we'll call when we've received a touch. Our view controller should implement it and hide the keyboard
- (void)pickerViewDidReceiveTouch:(UIPickerView *)pickerView;

@end

@interface MyPickerView : UIPickerView

// We're redefining delegate to require conformity to the MyPickerViewDelegate protocol we just made
@property (nonatomic, weak) id <MyPickerViewDelegate>delegate;

@end


//
//  MyPickerView.m
//

#import "MyPickerView.h"

@implementation MyPickerView
@synthesize delegate = _myPickerViewDelegate; // We changed the data type of delegate as it was declared in the superclass so it's important to link it to a differently named backing variable

- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event {
    // We make sure to call the super method so all standard functionality is preserved
    UIView *hitView = [super hitTest:point withEvent:event];

    if (hitView) {
        // This will be true if the hit was inside of the picker
        [_myPickerViewDelegate pickerViewDidReceiveTouch:self];
    }

    // Return our results, again as part of preserving our superclass functionality
    return hitView;
}

@end

次に、ViewControllerで、<MyPickerViewDelegate>の代わりに準拠するように変更します<UIPickerViewDelegate>MyPickerViewDelegate標準メソッドから継承しUIPickerViewDelegate、標準メソッドを通過するため、これは問題ありませんUIPickerViewDelegate

最後に、ViewControllerに実装pickerViewDidReceiveTouch:します。

- (void)pickerViewDidReceiveTouch:(UIPickerView *)pickerView {
    [enterInput resignFirstResponder];
}
于 2012-05-30T07:54:37.723 に答える