问题
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.
So, shall I query the database in viewDidLoad() function and then change the buttons' text in viewWillAppear according to the answer? Is that right?
回答1:
You can query a database whenever you want.
ViewDidLoad will only get called once per instance of your view controller. It is typically used for initializing objects.
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.
回答2:
yes you can query it in viewDidLoad but if you do it in viewWillAppear will be more good because viewDidLoad calls once when view loading first time and never called again until unless view will deloc but viewWillAppear call every time for example you have one view and it have navigation view inside and you navigate inside the other view so when you press back button viewDidLoad will not call but yes viewWillAppear will always call.
回答3:
Yes you can query in viewDidLoad and do UI modifications in viewDidAppear.
Actually it depends on your need. If your result will change every time OR if you wanna refresh data every time your View's display then query and modify UI in viewWillAppear OR viewDidAppear as viewDidLoad will query only once at the time your View first loads.
来源:https://stackoverflow.com/questions/32286330/ios-swift-in-which-function-between-viewdidload-and-viewwillappear-am-i-suppose