2

インターフェイスファイルにこれがあります:

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
    <UIPickerViewDataSource, UIPickerViewDataSource>

// PickerView Array
@property (strong, nonatomic) NSArray *pickerNames;
@property (strong, nonatomic) NSArray *pickerValues;    
@property (strong, nonatomic) IBOutlet UIPickerView *picker;

- (IBAction)buttonSelectPicker;

@end

そして私は実装ファイルにこれを持っています:

- (void)viewDidLoad
{
    [super viewDidLoad];

    // PickerView Array
    _pickerNames = @[@"Data1", @"Data2", @"Data3", @"Data4", @"Data5"];
    _pickerValues = @[@"val1", @"val2", @"val3", @"val4", @"val5"];
    _picker.frame = CGRectMake(0, 1000, 320, 216);
}

これらのコードはすべて、私の iPod (iOS 6.1.3) で完全に動作しますが、実際にはもっとやりたいことに問題があります...

  1. アプリが初めてロードされると、UIPickerView は非表示になります (画面領域の外側)。この行 (on ) を追加してみましたviewDidLoadが、うまくいきませんでした。

    _picker.frame = CGRectMake(0, 1000, 320, 216);

  2. ユーザーがbuttonSelectPickerボタンをタップすると、UIPickerView が表示されます。このコード行も機能しませんでした:

    - (IBAction)buttonSelectPicker { [UIView beginAnimations:nil context:NULL]; [UIView setAnimationDuration:0.3]; _pickerviewContainer.frame = CGRectMake(0, 289, 320, 216); [UIView commitAnimations]; }

  3. ユーザーが UIPickerView 領域外の任意の場所をタップすると、UIPickerView が非表示になります。キーボードを閉じるためのこのコードがありますが、 UIPickerView を閉じるように変更する方法がわかりません:

    - (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event { for (UIView * txt in self.view.subviews){ if ([txt isKindOfClass:[UITextField class]]) { [txt resignFirstResponder]; } } }

ありがとうございました...

4

2 に答える 2

3

あなたは基本的に何を再作成してinputViewいます。UIKit を使用すると、キーボードだけでなく、任意のカスタム入力メソッドを定義できます。したがって、 を配置して、次UITextFieldのように設定できます。inputView

UIPickerView *picker = [[UIPickerView alloc] initWithFrame:CGRectMake(0, 0, 320, 162)];

picker.dataSource = self;
picker.delegate = self;

self.textField.inputView = picker;

(ピッカーはペン先からロードすることもできます)

次に、テキスト フィールドの処理に通常の方法を使用します。

// Show keyboard/picker
[self.textField becomeFirstResponder]

// Dismiss keyboard/picker
[self.textField resignFirstResponder]

テキスト フィールドがファーストレスポンダになると、キーボードの代わりにピッカーが表示されます。これがあなたのニーズに合っているかどうかはわかりませんが、代替のよりクリーンな方法と考えてください。

于 2013-09-13T05:39:28.113 に答える
1

簡単に使用できます:

  self.dataPicker.hidden=NO; and 
  self.dataPicker.hidden=YES;

適切な状況で。

更新:別のビューで UIPicker を使用してから使用します

[UIView animateWithDuration:0.25 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
            self.infoView.frame = CGRectMake(0 , 70,  self.infoView.frame.size.width,  self.infoView.frame.size.height);

        }
                         completion:^(BOOL finished) {

                         }];

ここで、infoView は取得した新しい UIView の名前です。ビュー アニメーション メソッドを 2 回呼び出して非表示 (viewcontroller の外に移動) してビューを表示し、それに応じて X、Y 座標を変更する必要があります。

于 2013-09-13T05:18:31.760 に答える