【问题标题】:UIDatePicker will not animateUIDatePicker 不会设置动画
【发布时间】:2010-07-14 11:57:27
【问题描述】:

我有一个包含分段控制器和只显示 24 小时时间的 UIDatePicker 的视图。我希望能够根据分段控件的内容设置不同的时间。所以“Mon”对应一个时间,“Tue”对应另一个时间,以此类推。我有这个工作。但是,当用户更改分段控制器上的按钮时,我还想为选择器设置动画。截至目前,我的操作方法中已链接到分段控制器:

[timePicker setDate:date animate:YES];

其中 date 是一个 NSDate,格式如下:HH:mm

正如我所说,它将更改为日期,但不会为操作设置动画。有谁知道为什么它没有动画?

顺便说一句,虽然我已将格式化程序设置为“HH:mm”,但如果我使用 NSLog 打印它,我仍然会打印整个 1970-01-01 +02:00:00 字符串。我假设唯一的 HH:mm 无论如何都会发送到日期选择器,并且它只是打印整个内容的 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:,我假设这是一个错字)。
  • 您的“日期是 NSDate 格式如下:HH:mm”没有任何意义。 NSDate 总是代表一个特定的时间点,包括日期和时间直到 microsendend。 NSDate 对象本身根本没有固有的格式。

标签: iphone animation ios4 nsdate uidatepicker


【解决方案1】:

iOS4.0.1 SDK 对我来说也不是动画,无论是在模拟器还是设备上。不知道它是否在 4.0.1 之前工作。

要试用一下,请将以下代码添加到示例项目 UICatalog、PickerViewController.m 中 - (void)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];

还要在上面添加一个方法 - PickerViewController.m 中的 (void)viewDidLoad

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

根据代码,如果您运行项目,导航到 Pickers,选择 UIDatePicker,将日期轮旋转到今天以外的任何日期,点击“旋转我”按钮,它应该将轮设置回今天,但是它不会动画。

我很难弄清楚这是否只是一个错误,以及是否有任何解决方法。听到 setDate:animated: 对你们中的某些人有用,这让我感到非常惊讶。

【讨论】:

  • 我可以确认,我编写的一个 3.1 构建的应用程序在 3.1.x 上运行良好,在安装到 iOS 4 时确实不会转动轮子。作为一个快速破解,你能尝试一下吗?通过致电performSelector:withObject:afterDelay: 家庭中的一位来推迟设置日期?没关系,但它会将其从事件处理循环中移除。
  • 此问题已在 4.1 中修复。 @mvds:推迟不起作用,该错误与 setDate:animated: 4.0 和 4.0.1 中的方法有关
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-24
  • 1970-01-01
  • 1970-01-01
  • 2014-01-09
  • 1970-01-01
相关资源
最近更新 更多