如何像键盘一样呈现选择器视图?

时间:2011-06-07 17:30:03

标签: objective-c cocoa-touch ios uipickerview

当我按下按钮(就像键盘一样)时,我希望显示UIPickerView,然后当用户点击屏幕上的任何位置时,它就会消失。我怎样才能做到这一点?感谢。

更多背景:我在UITableViewCell中有一个名为months的UITextField。现在,对我来说最好的选择是在那里放置一个UIPikcerView,用户只需选择月份而不必输入它(这是更好的方式)。但UIPickerView在UITableViewCell中看起来很丑陋,更不用说我无法改变它的巨大尺寸。那么在这种情况下我该怎么办?

2 个答案:

答案 0 :(得分:13)

这就是你应该如何在UITableView中使用UIPickerView作为textField。

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
static NSString *CellIdentifier = @"Cell";

UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
    cell.selectionStyle= UITableViewCellSelectionStyleNone;
    UITextField *textField = [[UITextField alloc]initWithFrame:CGRectMake(110, 10, 185, 30)];
    textField.clearsOnBeginEditing = NO;
    textField.textAlignment = UITextAlignmentRight;
    textField.delegate = self;
    [cell.contentView addSubview:textField];
    //textField.returnKeyType = UIReturnKeyDone;
    cell.textLabel.backgroundColor = [UIColor clearColor];
    cell.detailTextLabel.backgroundColor = [UIColor clearColor];

        // if you want a UIPickerView for first row... then do the following //
        // Here.. packSizePickerView is an object of UIPickerView

   if (indexPath.row == 1)
  {
    textField.tag=0;
    textField.delegate= self;
    packSizePickerView.delegate = self;
    packSizePickerView = [[UIPickerView alloc] init];
    packSizePickerView.autoresizingMask=UIViewAutoresizingFlexibleWidth;
    packSizePickerView.showsSelectionIndicator=YES;
    textField.inputView  = packSizePickerView;
    }

    // if you want to select date / months  in row 2, go for UIDatePickerView //

    if (indexPath.row == 1)
    {
        textField.tag = 1;
        UIDatePicker *datePicker = [[UIDatePicker alloc] init];
        datePicker.datePickerMode = UIDatePickerModeDate;
       [datePicker addTarget:self action:@selector(datePickerValueChangedd:) forControlEvents:UIControlEventValueChanged];
        datePicker.tag = indexPath.row;
        textField.delegate= self;
        textField.inputView = datePicker;
        [datePicker release];

    }

}

// Configure the cell...

NSInteger row = [indexPath row];
cell.textLabel.text = [myArray objectAtIndex:row];

return cell;

}

对于datePickerValueChangedd

- (void)datePickerValueChangedd:(UIDatePicker*) datePicker{
UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(100, 50, 68, 68)];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
label.text = [NSString stringWithFormat:@"%@",[df stringFromDate:datePicker.date]];

    NSLog(@"I am inside the datePickerValueChanged   - - %@", label.text);
[df release];
    globalValue1 = label.text;

   // use a global variable to copy the value from the pickerView. //
   }    

现在在textFieldDidEndEditing:

-(void) textFieldDidEndEditing:(UITextField *)textField {
if (textField.tag ==0)
   [textField setText: globalValue0];

if (textField.tag ==1)
   [textField setText: globalValue1];

}

要重新签名UIDatePicker,请将UIView设置为UIControl和

resignFirstResponder

答案 1 :(得分:0)

我认为您需要将UIPickerView放在可视屏幕的“外部”,并在按下按钮时将其设置为动画。我不太确定什么是最好的摆脱它的方法,但你可以在选择器可见时听取父视图的点击,然后将其动画回来。