私は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]];
}
このような問題を回避するには、変更を行う必要があることをお知らせください。