我试图在标签上显示日期,我在故事板中为该视图显示了该日期,但每次我尝试更改日期时,它仅显示今天的日期... Plz帮助 在我的.h文件中
IBOutlet UILabel *datelabel;
UIActionSheet *pickerViewPopup;
UIDatePicker *pickerView;
-(IBAction)dateButtonClicked:(id)sender;
`
在我的.m文件中
-(IBAction)dateButtonClicked:(id)sender {
[self showPickerView];
}
-(void)showPickerView {
pickerViewPopup = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:nil destructiveButtonTitle:nil otherButtonTitles:nil];
pickerView = nil;
pickerView = [[UIDatePicker alloc] initWithFrame:CGRectMake(0, 44, 0, 0)];
((UIDatePicker*)pickerView).datePickerMode = UIDatePickerModeDateAndTime;
UIToolbar *pickerToolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 44)];
pickerToolbar.barStyle = UIBarStyleBlackOpaque;
[pickerToolbar sizeToFit];
NSMutableArray *barItems = [[NSMutableArray alloc] init];
UIBarButtonItem *cancelBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelButtonPressed:)];
[barItems addObject:cancelBtn];
UIBarButtonItem *flexSpace = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:nil];
[barItems addObject:flexSpace];
UIBarButtonItem *doneBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:self action:@selector(doneButtonPressed:)];
[barItems addObject:doneBtn];
[pickerToolbar setItems:barItems animated:YES];
[pickerViewPopup addSubview:pickerToolbar];
[pickerViewPopup addSubview:pickerView];
[pickerViewPopup showInView:self.view];
[pickerViewPopup setBounds:CGRectMake(0, 0, 320, 464)];
}
-(void)doneButtonPressed:(id)sender {
NSDate *myDate = pickerView.date;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"cccc, MMM d, hh:mm aa"];
NSString *prettyVersion = [dateFormat stringFromDate:myDate];
self.datelabel.text=prettyVersion;
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
-(void)cancelButtonPressed:(id)sender{
[pickerViewPopup dismissWithClickedButtonIndex:1 animated:YES];
}
答案 0 :(得分:0)
您已将datelabel
作为实例变量而不是@property。您无法使用self.datelabel
访问它。所以在doneButtonPressed:
...
更改以下行
self.datelabel.text=prettyVersion;
与
datelabel.text=prettyVersion;
BTW ...... XCode必须在这一行显示警告,而不是?