0

これは単純化されたコードです。テキスト フィールドのみのビュー コントローラーを使用して、新しい問題を反映するようにタイトルも更新しました。

実行時にテキスト フィールドをクリックすると、ピッカー ビューが表示されます。ピッカー ビューで値をクリックすると、テキスト フィールドに値が表示されます。ナビゲーション バーの [完了] ボタンを押すと、ピッカー ビューを閉じることができるようになりました。しかし、テキスト フィールドをもう一度クリックすると、ピッカー ビューがもう一度ポップアップしません。

輸入

@interface MyPickerViewViewController : UIViewController <UIPickerViewDelegate,
                                                      UIPickerViewDataSource,
                                                      UITextFieldDelegate>

@end


@interface MyPickerViewViewController () {
    UIPickerView *_pv;
    NSArray *_array;
    IBOutlet __weak UITextField *_tf;
}

@end

@implementation MyPickerViewViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    _array = [NSArray arrayWithObjects:@"One", @"Two", @"Three", nil];

// Do any additional setup after loading the view, typically from a nib.
    _pv = [[UIPickerView alloc] initWithFrame:CGRectZero];
    _pv.dataSource = self;
    _pv.delegate = self;
    _tf.inputView = _pv;

    UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 56)];
    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [mypickerToolbar sizeToFit];
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc]    initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
    [barItems addObject:doneBtn];
    [mypickerToolbar setItems:barItems animated:YES];
    _tf.inputAccessoryView = mypickerToolbar;

    _tf.delegate = self;

}

- (void)pickerDoneClicked:(id)sender
{
    [_pv removeFromSuperview];
    [_tf.inputView removeFromSuperview];
    [_tf.inputAccessoryView removeFromSuperview];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

// Added this method and now the picker view comes up every time I
// click on the text field, even after I dismiss it. However, the
// picker view is in wrong place, so I just have to adjust the
// CGRect assigned to it.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    [self.view addSubview:_tf.inputAccessoryView];
    [self.view addSubview:_tf.inputView];
    return NO;
}
- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    NSLog(@"Hello there");
}

- (void)textFieldDidEndEditing:(UITextField *)textField
{
    [textField resignFirstResponder];
}

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

- (void)selectRow:(NSInteger)row inComponent:(NSInteger)component animated:(BOOL)animated
{

}

- (NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(    NSInteger)component
{
    return [_array objectAtIndex:row];
}

- (NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView
{
    return 1;
}

- (NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component
{
    return _array.count;
}

- (void) pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component
{
    _tf.text = [_array objectAtIndex:row];
}    

@end
4

4 に答える 4

0

これは、機能したピッカービューとツールバーの調整済みCGRectです。ピッカービューのデフォルトの高さがあるかどうか疑問に思っていましたか?

- (void)viewDidLoad
{
    [super viewDidLoad];

    _array = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil];

// Do any additional setup after loading the view, typically from a nib.
    CGRect viewRect = [[UIScreen mainScreen] bounds];
    CGFloat pvHeight = 235;
    CGFloat pvYpos = viewRect.size.height - pvHeight;

    CGRect pvRect = CGRectMake(viewRect.origin.x, pvYpos, viewRect.size.width, pvHeight);
    _pv = [[UIPickerView alloc] initWithFrame:pvRect];
    _pv.dataSource = self;
    _pv.delegate = self;
    _tf.inputView = _pv;

    CGFloat tbYpos = pvYpos-44;
    UIToolbar*  mypickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, tbYpos, viewRect.size.width, 44)];
    mypickerToolbar.barStyle = UIBarStyleBlackOpaque;
    [mypickerToolbar sizeToFit];
    NSMutableArray *barItems = [[NSMutableArray alloc] init];
    UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
    [barItems addObject:flexSpace];
    UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(pickerDoneClicked:)];
    [barItems addObject:doneBtn];
    [mypickerToolbar setItems:barItems animated:YES];
    _tf.inputAccessoryView = mypickerToolbar;

    _tf.delegate = self;

}
于 2012-10-31T15:00:24.577 に答える
0

この追加による更新を参照してください

// Added this method and now the picker view comes up every time I
// click on the text field, even after I dismiss it. However, the
// picker view is in wrong place, so I just have to adjust the
// CGRect assigned to it.
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{    
    [self.view addSubview:_tf.inputAccessoryView];
    [self.view addSubview:_tf.inputView];
    return NO;
}  
于 2012-10-31T14:25:35.350 に答える
0

あなたは試すことができます:

[self.view endEditing:YES];

あなたのアクションメソッドで

于 2012-10-29T05:47:50.963 に答える
0

これを見逃したと思います:

[datePicker removeFromSuperview];
[pickerView removeFromSuperview];
于 2012-10-29T01:45:27.570 に答える