如何提出一个选择器视图就像键盘一样? [英] How can I present a picker view just like the keyboard does?

查看:92
本文介绍了如何提出一个选择器视图就像键盘一样?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个UIPickerView显示当我按下一个按钮(就像键盘),然后离开时,用户点击屏幕上的任何地方。我如何做到这一点?谢谢。

I want a UIPickerView to show up when I press a button (just like keyboard) and then go away when user taps anywhere on the screen. How can I do this? Thanks.

多一些背景:在UITableViewCell中有一个名为months的UITextField。现在对我来说最好的选择是在那里放一个UIPikcerView,它将更容易为用户选择月,而不必键入它(这是更好的方式)。但是UIPickerView在UITableViewCell中看起来真的很丑陋,更不用说我不能改变它的巨大的大小。那么我该怎么办呢?

A bit more background: I have a UITextField called months in UITableViewCell. Now the best option for me is to put a UIPikcerView there and it will be easier for the user to just chose the month rather than having to type it out (and it's the better way). But UIPickerView looks really ugly in a UITableViewCell, not to mention I can't change it's gigantic size. So what should I do in this case?

推荐答案

这是您应该如何使用UIPickerView的UITableView中的textField。

This is how you should use UIPickerView for a textField in a 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;

}

并且对于datePickerValueChangedd



And for 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中:

Now in the textFieldDidEndEditing:

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

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

}

要退出UIDatePicker,请将UIView设置为UIControl

And to resign UIDatePicker, set the UIView as a UIControl and

resignFirstResponder

这篇关于如何提出一个选择器视图就像键盘一样?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆