0

我以编程方式创建了一个 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];
4

1 回答 1

0

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];
}
于 2012-05-24T05:33:41.527 回答