J'ai une vue contenant un contrôleur segmenté et un UIDatePicker qui affiche seulement 24 heures. Je veux être en mesure de définir des heures différentes en fonction de ce que dit le contrôle segmenté. Donc "Mon" correspondrait à une fois, "Mar" à une autre et ainsi de suite. J'ai ce travail. Cependant, je voudrais également animer le sélecteur lorsque l'utilisateur change de bouton sur le contrôleur segmenté. A partir de maintenant j'ai dans ma méthode d'action liée au contrôleur segmenté ceci:UIDatePicker n'anime pas
[timePicker setDate:date animate:YES];
Si la date est un NSDate formaté comme ceci: HH: mm
Comme je l'ai dit, il va changer la date, mais il n'animera pas l'opération. Quelqu'un sait pourquoi il n'anime pas? Par ailleurs, bien que j'aie défini le format du formateur sur "HH: mm" si j'utilise NSLog pour l'imprimer, j'obtiens toute la chaîne 1970-01-01 +02: 00: 00 imprimée. Je suppose que le seul HH: mm est envoyé au datpicker de toute façon et que c'est juste NSLog qui imprime le tout?
EDIT: Voici le code pour le contrôleur de vue réelle, et cela est en cours d'exécution 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
Pouvez-vous poster un peu plus de code (en particulier comment vous créez les instances NSDate)? Parce que la seule ligne de code que vous avez posté semble bien (sauf que la méthode correcte est 'setDate: animated:', pas 'setDate: animate:', je suppose que c'est une faute de frappe). –
Votre "Où date est un NSDate formaté comme: HH: mm" n'a aucun sens. NSDate représente toujours un point spécifique dans le temps, y compris la date et l'heure jusqu'à la microsendonde. Un objet 'NSDate' seul n'a aucun format inhérent du tout. –