我有一個包含分段控制器和僅顯示24小時的UIDatePicker的視圖。我希望能夠根據分段控制的說明設定不同的時間。所以「星期一」對應一次,「星期二」對應另一次,等等。我有這個工作。但是,我還想在用戶更改分段控制器上的按鈕時爲選取器設置動畫。截至目前我已經在我的鏈接到分段控制器的操作方法是:UIDatePicker不會動畫
[timePicker setDate:date animate:YES];
凡日期是NSDate的格式像這樣:HH:MM
正如我所說的,它會改變的日期,但它不會動作操作。任何人都知道它爲什麼不動畫?
順便說一句,雖然我已經將格式化程序設置爲「HH:mm」,如果我使用NSLog打印它,仍然可以打印整個1970-01-01 +02:00:00字符串。我認爲唯一的HH:mm無論如何都被髮送到datepicker,並且它只是NSLog打印整個事情?
編輯:這是實際的視圖控制器的代碼,這是運行SDK4
#import "FlipsideViewController.h"
@implementation FlipsideViewController
@synthesize delegate, times, weatherImage, condition, timePicker;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor viewFlipsideBackgroundColor];
UIImage *image = [UIImage imageNamed:@"sunny.png"];
weatherImage.image = image;
WeatherClockAppDelegate *appDelegate = (WeatherClockAppDelegate *)[[UIApplication sharedApplication] delegate];
times = appDelegate.times;
[timePicker addTarget:self action:@selector(timePickerAction:) forControlEvents:UIControlEventValueChanged];
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSDate *sunnyDate = [df dateFromString: [times objectAtIndex:0]];
[df release];
[timePicker setDate:sunnyDate animated:NO];
}
- (IBAction)done:(id)sender {
if (!busy) {
[self.parentViewController dismissModalViewControllerAnimated:YES];
for (NSString *s in times) {
NSLog(@"Set times: %@", s);
}
}
}
- (IBAction)segmentedAction {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
switch (self.condition.selectedSegmentIndex) {
case 0:
self.weatherImage.image = [UIImage imageNamed:@"sunny.png"];
NSDate *sunnyDate = [df dateFromString: [times objectAtIndex:0]];
[timePicker setDate:sunnyDate animated:YES];
break;
case 1:
self.weatherImage.image = [UIImage imageNamed:@"cloudy.png"];
NSDate *cloudyDate = [df dateFromString: [times objectAtIndex:1]];
NSLog(@"Cloudydate: %@", cloudyDate);
[timePicker setDate:cloudyDate animated:YES];
break;
case 2:
self.weatherImage.image = [UIImage imageNamed:@"rain.png"];
NSDate *rainyDate = [df dateFromString: [times objectAtIndex:2]];
[timePicker setDate:rainyDate animated:YES];
break;
case 3:
self.weatherImage.image = [UIImage imageNamed:@"final.png"];
NSDate *finalDate = [df dateFromString: [times objectAtIndex:3]];
[timePicker setDate:finalDate animated:YES];
break;
}
[df release];
}
- (IBAction)timePickerAction:(id)sender {
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"HH:mm"];
NSString *date = [NSString stringWithFormat:@"%@", [df stringFromDate:timePicker.date]];
NSLog(@"%@", date);
[df release];
switch (self.condition.selectedSegmentIndex) {
case 0:
[times replaceObjectAtIndex:0 withObject:date];
break;
case 1:
[times replaceObjectAtIndex:1 withObject:date];
break;
case 2:
[times replaceObjectAtIndex:2 withObject:date];
break;
case 3:
[times replaceObjectAtIndex:3 withObject:date];
break;
}
}
- (void)didReceiveMemoryWarning {
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning];
// Release any cached data, images, etc that aren't in use.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
}
/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/
- (void)dealloc {
[times release];
[weatherImage release];
[condition release];
[timePicker release];
[super dealloc];
}
@end
你可以發佈一些更多的代碼(特別是你如何創建NSDate實例)?因爲你發佈的單行代碼看起來很好(除了正確的方法是'setDate:animated:',而不是'setDate:animate:',我假設這是一個錯字)。 – 2010-07-14 12:22:41
你的「哪裏的日期是NSDate格式如下:HH:mm」是沒有意義的。 NSDate總是代表一個特定的時間點,包括日期和時間,直到microsendond。單獨一個'NSDate'對象根本沒有固有的格式。 – 2010-07-14 13:27:11