gpt4 book ai didi

objective-c - 如何像键盘一样呈现选择器 View ?

转载 作者:可可西里 更新时间:2023-11-01 03:28:15 25 4
gpt4 key购买 nike

我希望 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;

对于 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

关于objective-c - 如何像键盘一样呈现选择器 View ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6269244/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com