UIDatePicker 不隐藏
Posted
技术标签:
【中文标题】UIDatePicker 不隐藏【英文标题】:UIDatePicker not hiding 【发布时间】:2012-05-24 05:19:22 【问题描述】:我以编程方式创建了一个 UIDatePicker,并通过一种方法将所选值设置为文本字段。但是该值没有进入文本字段并且选择器没有隐藏。谁能帮我?我的代码在这里
datePicker = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 245, 320, 270)];
[self.view addSubview:datePicker];
datePicker.hidden=YES;
[datePicker release];
datePicker.datePickerMode = UIDatePickerModeDate;
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"yyyy:MMMM:dd"];
self.dobtxt.text = [outputFormatter stringFromDate:self.datePicker.date];
[outputFormatter release];
[DoneBtnDate setBackgroundImage: [UIImage imageNamed: @"OTdone.png"] forState:UIControlStateNormal];
PickerDoneviewDate=[[UIView alloc]initWithFrame:CGRectMake(0,210, 320, 35)];
PickerDoneviewDate.backgroundColor=[UIColor clearColor];
PickerDoneviewDate.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"MFC_tab_01.png"]];
DoneBtnDate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[DoneBtnDate addTarget:self action:@selector(DatePickerDone:)forControlEvents:UIControlEventTouchDown];
DoneBtnDate.frame = CGRectMake(245,5, 70, 30);
[PickerDoneviewDate addSubview:DoneBtnDate];
[self.view addSubview:PickerDoneviewDate];
PickerDoneviewDate.hidden=YES;
-(void)DatePickerDone:(id)sender
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"yyyy:MMMM:dd"];
self.dobtxt.text = [outputFormatter stringFromDate:self.datePicker.date];
[datePicker release];
[datePicker setHidden:TRUE];
PickerDoneviewDate.hidden=YES;
[outputFormatter release];
添加文本字段的代码
dobtxt = [[UITextField alloc] initWithFrame:CGRectMake(150, 120, 133, 26)];
dobtxt.borderStyle = UITextBorderStyleNone;
dobtxt.background=[UIImage imageNamed: @"textbox_small.png"];
dobtxt.font = [UIFont fontWithName:@"Arial" size:13];
dobtxt.delegate = self;
dobtxt.userInteractionEnabled=YES;
dobtxt.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
[scroll addSubview:dobtxt];
【问题讨论】:
添加 self.dobtxt.text = @"test"; [滚动 addSubview:dobtxt] 之前;检查测试是否会在那里。 是的,它在该文本字段中显示测试 如何替换“self.dobtxt.text = [outputFormatter stringFromDate:self.datePicker.date];”到 "self.dobtxt.text = @"test""? 没有它是空的......即使我删除了自我,它也是空的 只是 NSLog(@"%@", self.dobtxt);在两个地方检查是否有相同的实例。 【参考方案1】:datePicker 是一个属性,它会被自动释放。请删除
[datePicker release];
self.datePicker = [[[UIDatePicker alloc] initWithFrame:CGRectMake(0, 245, 320, 270)] autorelease];
[self.view addSubview:datePicker];
self.datePicker.hidden=YES;
//[datePicker release];
datePicker.datePickerMode = UIDatePickerModeDate;
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"yyyy:MMMM:dd"];
self.dobtxt.text = [outputFormatter stringFromDate:self.datePicker.date];
[outputFormatter release];
[DoneBtnDate setBackgroundImage: [UIImage imageNamed: @"OTdone.png"] forState:UIControlStateNormal];
PickerDoneviewDate=[[UIView alloc]initWithFrame:CGRectMake(0,210, 320, 35)];
PickerDoneviewDate.backgroundColor=[UIColor clearColor];
PickerDoneviewDate.backgroundColor=[UIColor colorWithPatternImage:[UIImage imageNamed:@"MFC_tab_01.png"]];
DoneBtnDate = [UIButton buttonWithType:UIButtonTypeRoundedRect];
[DoneBtnDate addTarget:self action:@selector(DatePickerDone:)forControlEvents:UIControlEventTouchDown];
DoneBtnDate.frame = CGRectMake(245,5, 70, 30);
[PickerDoneviewDate addSubview:DoneBtnDate];
[self.view addSubview:PickerDoneviewDate];
self.PickerDoneviewDate.hidden=YES;
-(void)DatePickerDone:(id)sender
NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
[outputFormatter setDateFormat:@"yyyy:MMMM:dd"];
self.dobtxt.text = [outputFormatter stringFromDate:self.datePicker.date];
//[datePicker release];
[datePicker setHidden:TRUE];
PickerDoneviewDate.hidden=YES;
[outputFormatter release];
【讨论】:
@priya 你对这两个版本发表评论了吗? 是评论了两行仍然文本字段为空并且日期选择器没有隐藏 @priya dobtxt 是一个出口吗?你连接到视图了吗? dobtext 是以编程方式创建的文本字段。它在滚动视图中... [scroll addSubview:dobtxt]; 你有 NSLog [outputFormatter stringFromDate:self.datePicker.date] 来检查是否有任何字符串输出?以上是关于UIDatePicker 不隐藏的主要内容,如果未能解决你的问题,请参考以下文章