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

2024-06-05

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

更多背景信息:我在 UITableViewCell 中有一个名为 Months 的 UITextField。现在对我来说最好的选择是在那里放置一个 UIPikcerView ,用户可以更轻松地选择月份,而不必输入它(这是更好的方法)。但是 UIPickerView 在 UITableViewCell 中看起来真的很难看,更不用说我无法改变它巨大的尺寸。那么这种情况我该怎么办呢?


这就是您应该如何将 UIPickerView 用于 UITableView 中的文本字段。

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

}

对于 datePicker ValueChanged

- (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
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何像键盘一样呈现选择器视图? 的相关文章

随机推荐