iOS/Swift:我应该在viewDidLoad和viewWillAppear之间的哪个函数中查询数据库? [英] iOS/Swift: in which function between viewDidLoad and viewWillAppear am I supposed to query a database?

查看:301
本文介绍了iOS/Swift:我应该在viewDidLoad和viewWillAppear之间的哪个函数中查询数据库?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中(由Swift和XCode 6开发),我必须查询数据库,然后根据该答案进行修改并显示一些UI小部件,在我的情况下,它们是三个彩色按钮,我必须更改包含数字的文字. 这些按钮指示分配给用户的任务数量,并且根据任务的优先级使用不同的颜色.

In my app, being developed by Swift and XCode 6, I have to query a database and according to that answer then I'll modify and show some UI widgets which, in my case, are three colored buttons whose I have to change their text which consists in a number. These buttons indicate the number of tasks assigned to an user and they have different colors according to the tasks' priority.

那么,我是否应该根据答案在viewDidLoad()函数中查询数据库,然后在viewWillAppear中更改按钮的文本?是吗?

So, shall I query the database in viewDidLoad() function and then change the buttons' text in viewWillAppear according to the answer? Is that right?

推荐答案

您可以随时查询数据库.

You can query a database whenever you want.

ViewDidLoad只会在每个视图控制器实例中被调用一次.它通常用于初始化对象.

ViewDidLoad will only get called once per instance of your view controller. It is typically used for initializing objects.

在视图中[显示/显示],您通常会通过设置标签文本和图像查看图像来更新UI,以反映您拥有的最新数据.

In view[Will/Did]Appear you typically update your UI to reflect the newest data you have by setting label text and image view images.

如果您不需要在每次显示视图时都重新查询数据库,那么您描述的方法是正确的.

Your described approach is correct if you don't need to re-query the database every time a view appears.

这篇关于iOS/Swift:我应该在viewDidLoad和viewWillAppear之间的哪个函数中查询数据库?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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