菜鸟教程小白 发表于 2022-12-12 18:17:51

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


                                            <p><p>我有一个案例,用户需要在每天早上 8 点之前提交表单。</p>

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

<p>早上 8 点之后,我想通过隐藏覆盖 View 来清除表单并让表单 View 再次可用。如果可能,我想在早上 8 点的某个时间点执行此操作。</p>

<p>我正在考虑使用 <code>NSTimer</code> 来完成此任务。我可以设置一个将在早上 8 点运行的计时器来清除表单并移除覆盖 View 吗?</p>

<p>有没有更好的方法来处理这种情况?</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>您可以使用本地推送通知。官方文档在这里
<a href="https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html" rel="noreferrer noopener nofollow">https://developer.apple.com/library/ios/documentation/NetworkingInternet/Conceptual/RemoteNotificationsPG/Introduction.html</a> </p>

<p>类似的东西:</p>

<pre><code>UILocalNotification *localNotification = [ init];
localNotification.fireDate = ; // Replace me with your &#34;event&#34; date
localNotification.timeZone = ;
localNotification.repeatInterval = kCFCalendarUnitDay;
[ scheduleLocalNotification:localNotification];
</code></pre>

<p>然后您可以按照文档中的说明“捕捉”通知</p>

<blockquote>
<p>If your app is frontmost, the application:didReceiveRemoteNotification: or <em>application:didReceiveLocalNotification:method</em> 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 <em>application:didFinishLaunchingWithOptions:</em> of your app delegate for either the <em>UIApplicationLaunchOptionsLocalNotificationKey</em> or UIApplicationLaunchOptionsRemoteNotificationKey key.</p>
</blockquote>

<p>您不能只使用 NSTimer,因为当您的应用关闭时它不会运行,但是涉及 NSTimer 的另一个解决方案是为事件日期创建一个 NSTimer,然后观察 <em>UIApplicationDidEnterBackgroundNotification</em> ,这里如果定时器正在运行,你将停止定时器,将 timer.fireDate 持久化(你可以将它保存在 NSUserDefault 中),最后观察 <em>UIApplicationDidBecomeActiveNotification</em>,你可以重新创建定时器(减去当前时间与您之前保存的值),或者如果事件时间已经过去,则只触发操作。</p></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 如何在 iOS 中的特定时间每天更改 ViewController ,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/21414437/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/21414437/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 如何在 iOS 中的特定时间每天更改 View Controller