2010-07-14 87 views
0

我有一個包含分段控制器和僅顯示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 
+0

你可以發佈一些更多的代碼(特別是你如何創建NSDate實例)?因爲你發佈的單行代碼看起來很好(除了正確的方法是'setDate:animated:',而不是'setDate:animate:',我假設這是一個錯字)。 – 2010-07-14 12:22:41

+0

你的「哪裏的日期是NSDate格式如下:HH:mm」是沒有意義的。 NSDate總是代表一個特定的時間點,包括日期和時間,直到microsendond。單獨一個'NSDate'對象根本沒有固有的格式。 – 2010-07-14 13:27:11

回答

1

這不是動畫對我來說太上iOS4.0.1 SDK,無論是在模擬器和設備。不知道它是否在4.0.1之前工作。

要嘗試一下,添加以下代碼示例項目UICatalog,PickerViewController.m在 - (無效)viewDidLoad中

UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect]; 
button.frame = labelFrame; 
[button setTitle:@"Spin me" forState:UIControlStateNormal]; 
[button addTarget:self action:@selector(spin) forControlEvents:UIControlEventTouchUpInside]; 
[self.view addSubview:button]; 

添加也高於一個方法 - (無效)viewDidLoad中的PickerViewController.m

- (void) spin { 
    [datePickerView setDate:[NSDate date] animated:YES]; 
} 

基礎上的代碼,如果你運行該項目,導航到拾貝,選擇的UIDatePicker,旋日期輪比當今其他任何日期,點擊「自旋我」按鈕,它應該設置輪迴今天,但它不會動畫。

我很難搞清楚這是否只是一個錯誤,以及是否有任何解決方法。非常驚喜聽到setDate:animated:正在爲你們中的一些人工作。

+0

對!這是一個謎! – sebrock 2010-07-25 21:14:35

+0

我可以證實,我編寫的一個3.1版本的應用程序,在3.1.x上運行得非常好,在iOS 4中安裝時確實不會旋轉車輪。作爲一種快速入侵,您是否可以嘗試並稍微延遲設置日期,通過從'performSelector:withObject:afterDelay:'family調用一個?應該沒關係,但它會將其從事件處理循環中取出。 – mvds 2010-07-26 08:45:33

+0

此問題已在4.1中修復。 @mvds:推遲不起作用,該錯誤與4.0和4.0.1中的setDate:animated:方法有關 – junjie 2010-10-18 11:48:44