1

I have an app that gives you a quote everyday in a notification and displays it with a label in the main view controller. It detects with a dateHasChanged()-function whether it's still the same day or not.

Here's my problem: if I (for example) have used the app on the first day and did not close it properly, then locked my smartphone so that it remains active until the next day, then unlocked my smartphone, my app would still display the old quote even though in the viewDidLoad() it says to check if the date has changed and optionally changed the label text to the new quote. To solve this problem, I already wrote a function in the main view controller like that:

func refresh() {
     viewDidLoad()
}

and called it from the appDelegate (didFinishLaunchingWithOptions) like that:

let mvc = MainViewController() 
mvc.refresh()

But it just gives me an error when running it. How can I refresh the screen after my app remained active in the background for a longer time?

Thank you for your help!

Whazzup
  • 175
  • 2
  • 12

2 Answers2

1

viewDidLoad is usually for overriding and not to call directly. The right spot to refresh is in AppDelegate 's :

func applicationDidBecomeActive(_ application: UIApplication)

or add your viewController as observer for application events as detailed here

Community
  • 1
  • 1
janusfidel
  • 8,036
  • 4
  • 30
  • 53
0

You never call the viewDidLoad() method!! The system calls it when the view initially loads. To answer your question, you can override viewWillAppear() (this will be called when the app has been in the background and is opened). To refresh the content, it depends on how you display your content. If you use a UITableView, then call tableView.reloadData().

Hope that helps!