我有一个包含分段控制器的视图和一个仅显示24小时时间的UIDatePicker。我希望能够根据分段控件所说的设置不同的时间。因此,“星期一”将对应一次,“星期二”对应于另一次,依此类推。我有这个工作。但是,当用户更改分段控制器上的按钮时,我还想为选取器设置动画。截至目前,我已将我的动作方法链接到分段控制器:
[timePicker setDate:date animate:YES];
其中date是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
答案 0 :(得分:1)