ios - 如何在 iOS 的特定时间每天更改 View Controller

标签 ios objective-c cocoa-touch

我有一个用户需要在每天早上 8 点之前提交表单的情况。

当他们提交时,会覆盖一个确认 View 以向用户显示他们已提交并阻止他们再次提交。

早上 8 点后,我想清除表单并通过隐藏重叠 View 使表单 View 再次可用。如果可能的话,我想在早上 8 点的某个时间点执行此操作。

我正在考虑用 NSTimer 来完成这个。我可以设置一个在早上 8 点运行的计时器来清除表单并删除叠加 View 吗?

有没有更好的方法来处理这种情况?

最佳答案

您可以使用本地推送通知。官方文档在这里 https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html

类似的东西:

UILocalNotification *localNotification = [[UILocalNotification alloc] init];
localNotification.fireDate = [NSDate date]; // Replace me with your "event" date
localNotification.timeZone = [NSTimeZone localTimeZone];
localNotification.repeatInterval = kCFCalendarUnitDay;
[[UIApplication sharedApplication] scheduleLocalNotification:localNotification];

然后你就可以像文档所说的那样“捕获”通知

If your app is frontmost, the application:didReceiveRemoteNotification: or application:didReceiveLocalNotification:method is called on its app delegate If your app is not frontmost or not running you handle the notifications by checking the options dictionary passed to the application:didFinishLaunchingWithOptions: of your app delegate for either the UIApplicationLaunchOptionsLocalNotificationKey or UIApplicationLaunchOptionsRemoteNotificationKey key.

您不能只使用 NSTimer,因为它不会在您的应用程序关闭时运行,但是涉及 NSTimer 的另一种解决方案是为事件日期创建一个 NSTimer,然后观察 UIApplicationDidEnterBackgroundNotification ,这里如果定时器正在运行,你将停止定时器,持久化 timer.fireDate(例如你可以将它保存在 NSUserDefault 中),最后观察 UIApplicationDidBecomeActiveNotification,你可以重新创建定时器(减去当前时间与您之前保存的值)或者如果事件时间已经过去则触发操作。

关于ios - 如何在 iOS 的特定时间每天更改 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21414437/

相关文章:

iphone - 在 .m 上声明委托(delegate)

ios - 导航栏上的另一个按钮在哪里

objective-c - 多个对象订阅 NSNotification (Objective-c)

ios - 通过程序开发多个图表

ios - UITabBarController 等待加载 UIViewController 直到选择 Xamarin

objective-c - 如何以编程方式移动到另一个 UIViewController?

iphone - 如何在 iphone 中设置 UIViewController 的背景颜色?

ios - 让UIView不断淡入淡出——完成:^{ possible infinite loop?

iphone - UITabBar 选定的色调颜色

iphone - iOS7 状态栏像原生天气应用