每当视图重新加载或一段时间后刷新 iOS UIDatePicker? [英] Refresh iOS UIDatePicker whenever a view is reloaded or after period of time?

查看:14
本文介绍了每当视图重新加载或一段时间后刷新 iOS UIDatePicker?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

当您最初启动应用程序时 - 视图首先加载;在 viewDidLoad 中,我将 UIDatePicker 设置为当前日期/时间.它工作正常.

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

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

有什么想法吗?

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

有什么想法吗?

解决方案

您的视图确实加载可能看起来像这样

- (void)viewDidLoad {[超级viewDidLoad];//监听应用程序何时激活的通知并刷新日期选择器[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshDatePicker) name:UIApplicationDidBecomeActiveNotification object:nil];}

然后在您的 viewWillAppear 中:

- (void)viewWillAppear:(BOOL)animated {[super viewWillAppear:animated];//当视图即将出现时刷新日期选择器//第一次或者如果我们从另一个视图切换到这个视图[自刷新日期选择器];}

并简单地实现刷新方法:

- (void)refreshDatePicker {//在选择器上设置当前日期[self.datepicker setDate:[NSDate 日期]];}

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

I've got an iPhone app that I've made, that on one view has a UIDatePicker.

When you initially launch the app - and the view first loads; inside the viewDidLoad I have the UIDatePicker get set to the current date/time. It works fine.

Now if the user minimizes the app and does other things (but does not kill the app) and then goes back to the app, the date/time does not update when you go back to that view.

I'm wondering how I would go about making it 'refresh' that UIDatePicker any time the view is loaded (for example, when you go back to it after it's already been opened but is sitting in the background).

Any thoughts?

If there's no quick/easy way - I also considered creating a time related variable when the UIDatePicker loads initially - then when it is reloaded having it check to see if more than 10 minutes had passed since the last view. If so, then it would set the UIDatePicker to current date/time.

Any ideas?

解决方案

Your view did load could look something like this

- (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];
}

Then in your viewWillAppear:

- (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];
}

And implement the refresh method simply as:

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

This will update your date picker in both cases, when the view is switched from another view to this view while the application is open, and when the app is backgrounded and comes to the foreground with that view already open.

这篇关于每当视图重新加载或一段时间后刷新 iOS UIDatePicker?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆