0

我正在尝试在我在情节提要中为该视图设置的标签上显示日期,但是每次我尝试更改日期时,它只显示今天的日期...请在我的 .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];
}
4

1 回答 1

0

您已将datelabel其视为实例变量而不是@property。您无法使用self.datelabel. 所以在你的doneButtonPressed:...

更改以下行

self.datelabel.text=prettyVersion;

datelabel.text=prettyVersion;

顺便说一句... XCode 必须在这一行显示警告,不是吗?

于 2012-12-15T12:41:18.540 回答