我是IOS开发的新手。我正在开发一个应用程序,该应用程序需要用户填写表格以获取用户详细信息。详细信息包括我使用 UIDatePicker 的出生日期字段。我正在以编程方式添加那些 UIDatePicker 和 UIActionSheet,并希望更新标签上的日期。日期正在更新,但它们在同一标签上被覆盖。
这是代码
-(IBAction)dateButtonClicked:(id)sender {
actionSheet = [[UIActionSheet alloc] initWithTitle:@"Choose Date"delegate:self cancelButtonTitle:@"cancel" destructiveButtonTitle:nil otherButtonTitles:@"Select",nil];
[actionSheet showInView:self.view ];
[actionSheet setFrame:CGRectMake(0, 117, 320, 383)];
}
- (void)willPresentActionSheet:(UIActionSheet *)actionSheet {
datePickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 40, 320, 216)];
[datePickerView setMinuteInterval:5];
[datePickerView setTag: kDatePickerTag];
[actionSheet addSubview:datePickerView];
datelabel = [[UILabel alloc] init];
datelabel.frame = CGRectMake(55, 92, 300, 50);
datelabel.backgroundColor = [UIColor clearColor];
datelabel.textColor = [UIColor blackColor];
datelabel.font = [UIFont fontWithName:@"Verdana-Bold" size: 12.0];
datelabel.textAlignment = UITextAlignmentCenter;
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat: @"MM/dd/YYYY h:mm a"];
datelabel.text = [NSString stringWithFormat:@"%@",
[formatter stringFromDate:[NSDate date]]];
[self.view addSubview:datelabel];
NSArray *subviews = [actionSheet subviews];
[[subviews objectAtIndex:SelectButtonIndex] setFrame:CGRectMake(20, 266, 280, 46)];
[[subviews objectAtIndex:CancelButtonIndex] setFrame:CGRectMake(20, 317, 280, 46)];
}
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex != [actionSheet cancelButtonIndex]) {
[datePickerView addTarget:self
action:@selector(LabelChange:)
forControlEvents:UIControlEventValueChanged];
// UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Date" message:msg delegate:nil cancelButtonTitle: @"Dismiss" otherButtonTitles:nil];
// [alert show];
}
}
- (void)LabelChange:(id)sender{
NSDateFormatter *df = [[NSDateFormatter alloc] init];
df.dateStyle = NSDateFormatterMediumStyle;
datelabel.text = [NSString stringWithFormat:@""];
datelabel.text = [NSString stringWithFormat:@"%@",
[df stringFromDate:datePickerView.date]];
}
请让我知道需要进行更改才能使此类问题无效。