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

请参阅此添加的更新

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

这是一个调整后的 CGRect 用于选择器视图和工具栏的工作。我想知道pickerview是否有默认高度?

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

你可以试试:

[self.view endEditing:YES];

在你的行动方法中

于 2012-10-29T05:47:50.963 回答
0

我想你错过了这个:

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