在应用程序进入前台时刷新视图控制器内容
我有一个带有选项卡栏的应用程序,其中有几个带有用户生成内容的选项卡。当每个视图控制器加载时,viewDidLoad 方法向服务器发出 HTTP 请求以获取数据,然后相应地填充其视图。然而,一旦加载了所有数据,视图将保持不变(除非卸载并重新加载视图控制器)。
许多具有用户生成内容的其他应用程序也具有类似的行为,只是如果应用程序进入后台然后再次返回前台,每个视图都会重新加载。也就是说,当用户在应用程序处于活动状态时在各个选项卡之间切换时,内容不会自动刷新。如果用户“退出”应用程序然后稍后返回,视图将重新加载。
是否有设计应用程序以这种方式运行的标准实践?具体来说,我想知道是否有 UIKit 的优点可以用来刷新我对应用程序“重新启动”的看法。
否则,我想一个简单的方法是让每个视图控制器在 viewDidLoad 中注册通知并监听进入前台的应用程序。然后控制器可以通过重新加载其数据来响应每个通知。
I have an app with a tab bar that has a few tabs with user-generated content. When each view controller loads, the viewDidLoad method makes HTTP requests to a server to fetch data, then populates its view correspondingly. Once all the data is loaded, however, the view remains the same (unless the view controller is unloaded and reloaded).
A lot of other apps with user-generated content has a similar behavior, except each view will reload if the app goes into the background then comes back into the foreground again. That is, as the user switches between the various tabs while the app is active, the content won't be automatically refreshed. If the user "quits" the app then comes back to it later, the views will be reloaded.
Are there standard practices for designing an app to behave this way? Specifically, I'm wondering if there are UIKit niceties that I can take advantage of to refresh my views on app "restarts".
Otherwise, I guess a straightforward approach is to have each view controller register for notifications in viewDidLoad and listen for the app entering the foreground. The controller can then respond to each notification by reloading its data.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在每个视图控制器中,您希望订阅
UIApplicationDidBecomeActiveNotification
通知,并实现一个在收到该通知时调用的方法。此方法将重新加载,但是您需要重新加载的数据确实超出了我们在这里可以回答的范围。这就是我的做法,也是我建议的做法。In each of the View Controllers, you want to subscribe to the
UIApplicationDidBecomeActiveNotification
notification, and implement a method that gets called when it receives that notification. This method will do the reloading however your data needs to be reloaded is really beyond the scope of what we can answer here. This is how I do it, and how I recommend doing it.