每次重新加载视图或在一段时间后刷新iOS UIDatePicker?

每次重新加载视图或在一段时间后刷新iOS UIDatePicker?,ios,objective-c,uidatepicker,Ios,Objective C,Uidatepicker,我制作了一个iPhone应用程序,在一个视图上有一个UIDatePicker 当您最初启动应用程序时,首先加载视图;在viewDidLoad中,我将UIDatePicker设置为当前日期/时间。它很好用 现在,如果用户最小化应用程序并执行其他操作(但不会终止应用程序),然后返回到应用程序,则当您返回到该视图时,日期/时间不会更新 我想知道在任何时候加载视图时(例如,当您在视图已经打开但位于后台后返回视图时),如何让UIDatePicker“刷新” 有什么想法吗 如果没有快速/简单的方法——我也考

我制作了一个iPhone应用程序,在一个视图上有一个UIDatePicker

当您最初启动应用程序时,首先加载视图;在viewDidLoad中,我将UIDatePicker设置为当前日期/时间。它很好用

现在,如果用户最小化应用程序并执行其他操作(但不会终止应用程序),然后返回到应用程序,则当您返回到该视图时,日期/时间不会更新

我想知道在任何时候加载视图时(例如,当您在视图已经打开但位于后台后返回视图时),如何让UIDatePicker“刷新”

有什么想法吗

如果没有快速/简单的方法——我也考虑过在UIDatePicker最初加载时创建一个与时间相关的变量——那么当它重新加载时,让它检查自上次查看以来是否超过了10分钟。如果是这样,那么它会将UIDatePicker设置为当前日期/时间


有什么想法吗?

您应该能够在viewwillispect方法中设置日期。每次视图出现在屏幕上时都会调用该方法

- (void) viewWillAppear: (BOOL) animated 
{
    [super viewWillAppear:animated];
    // update the date in the datepicker
    [self.datepicker setDate:[NSDate date]];
}

您应该能够在ViewWillAppeard方法中设置日期。每次视图出现在屏幕上时都会调用该方法

- (void) viewWillAppear: (BOOL) animated 
{
    [super viewWillAppear:animated];
    // update the date in the datepicker
    [self.datepicker setDate:[NSDate date]];
}

您应该能够在ViewWillAppeard方法中设置日期。每次视图出现在屏幕上时都会调用该方法

- (void) viewWillAppear: (BOOL) animated 
{
    [super viewWillAppear:animated];
    // update the date in the datepicker
    [self.datepicker setDate:[NSDate date]];
}

您应该能够在ViewWillAppeard方法中设置日期。每次视图出现在屏幕上时都会调用该方法

- (void) viewWillAppear: (BOOL) animated 
{
    [super viewWillAppear:animated];
    // update the date in the datepicker
    [self.datepicker setDate:[NSDate date]];
}

您的视图可能看起来像这样

- (void)viewDidLoad {
    [super viewDidLoad];

    // listen for notifications for when the app becomes active and refresh the date picker
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDatePicker) name:UIApplicationDidBecomeActiveNotification object:nil];
}
然后在您的视图中将显示:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // refresh the date picker when the view is about to appear, either for the
    // first time or if we are switching to this view from another
    [self refreshDatePicker];
}
并将刷新方法简单地实现为:

- (void)refreshDatePicker {
    // set the current date on the picker
    [self.datepicker setDate:[NSDate date]];
}

在这两种情况下,当应用程序打开时视图从另一个视图切换到此视图时,以及当应用程序处于后台并在该视图已打开的情况下出现在前台时,这都会更新日期选择器。

您的视图加载可能与此类似

- (void)viewDidLoad {
    [super viewDidLoad];

    // listen for notifications for when the app becomes active and refresh the date picker
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDatePicker) name:UIApplicationDidBecomeActiveNotification object:nil];
}
然后在您的视图中将显示:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // refresh the date picker when the view is about to appear, either for the
    // first time or if we are switching to this view from another
    [self refreshDatePicker];
}
并将刷新方法简单地实现为:

- (void)refreshDatePicker {
    // set the current date on the picker
    [self.datepicker setDate:[NSDate date]];
}

在这两种情况下,当应用程序打开时视图从另一个视图切换到此视图时,以及当应用程序处于后台并在该视图已打开的情况下出现在前台时,这都会更新日期选择器。

您的视图加载可能与此类似

- (void)viewDidLoad {
    [super viewDidLoad];

    // listen for notifications for when the app becomes active and refresh the date picker
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDatePicker) name:UIApplicationDidBecomeActiveNotification object:nil];
}
然后在您的视图中将显示:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // refresh the date picker when the view is about to appear, either for the
    // first time or if we are switching to this view from another
    [self refreshDatePicker];
}
并将刷新方法简单地实现为:

- (void)refreshDatePicker {
    // set the current date on the picker
    [self.datepicker setDate:[NSDate date]];
}

在这两种情况下,当应用程序打开时视图从另一个视图切换到此视图时,以及当应用程序处于后台并在该视图已打开的情况下出现在前台时,这都会更新日期选择器。

您的视图加载可能与此类似

- (void)viewDidLoad {
    [super viewDidLoad];

    // listen for notifications for when the app becomes active and refresh the date picker
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDatePicker) name:UIApplicationDidBecomeActiveNotification object:nil];
}
然后在您的视图中将显示:

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];

    // refresh the date picker when the view is about to appear, either for the
    // first time or if we are switching to this view from another
    [self refreshDatePicker];
}
并将刷新方法简单地实现为:

- (void)refreshDatePicker {
    // set the current date on the picker
    [self.datepicker setDate:[NSDate date]];
}

在这两种情况下,当应用程序打开时视图从另一个视图切换到此视图时,以及当应用程序处于后台并在该视图已打开的情况下进入前台时,这都会更新您的日期选择器。

据我所知,如果视图出现在屏幕上,并且应用程序是先背景后前景的,则不会调用ViewWillAppeal。Good call@Mike,你的解决方案是更好的。据我回忆,如果视图出现在屏幕上,并且应用程序是先背景后前景的,则不会调用ViewWillAppeal。Good call@Mike,你的解决方案是更好的。据我回忆,如果视图在屏幕上,并且应用程序是背景的,然后又是前景的,则不会调用ViewWillAppeal。Good call@Mike,你的解决方案是更好的。据我回忆,如果视图显示在屏幕上,并且应用程序是背景的,然后又是前景的,则不会调用ViewWillAppeal。Good call@Mike,你的解决方案是更好的。这非常有效-谢谢!我有点不知所措——担心如果人们输入以前日期的数据(这是一个旅行日志应用程序)他们可能会离开屏幕去编辑一些东西,然后回来重新设置日期——但这一小小的不便是值得的,因为人们可能会完全输入错误的日期,因为他们忘记了关闭应用程序和/或在输入信息之前检查日期选择器。再次感谢!没问题,很乐意帮忙!这很有效-谢谢!我有点不知所措——担心如果人们输入以前日期的数据(这是一个旅行日志应用程序)他们可能会离开屏幕去编辑一些东西,然后回来重新设置日期——但这一小小的不便是值得的,因为人们可能会完全输入错误的日期,因为他们忘记了关闭应用程序和/或在输入信息之前检查日期选择器。再次感谢!没问题,很乐意帮忙!这很有效-谢谢!我有点不知所措——担心如果人们输入以前日期的数据(这是一个旅行日志应用程序)他们可能会离开屏幕去编辑一些东西,然后回来重新设置日期——但这一小小的不便是值得的,因为人们可能会完全输入错误的日期,因为他们忘记了关闭应用程序和/或在输入信息之前检查日期选择器。再次感谢!没问题,很乐意帮忙!这很有效-谢谢!我有点不知所措——担心如果人们输入以前日期的数据(这是一个旅行日志应用程序)他们可能会离开屏幕去编辑一些东西,然后回来重新设置日期——但这一小小的不便是值得的,因为人们可能会完全输入错误的日期,因为他们忘记了关闭应用程序和/或在输入信息之前检查日期选择器。再次感谢!没问题,很乐意帮忙!