Cristian C.
Cristian C.

Reputation: 302

Swift - func viewWillAppear

I have a standard SingleViewApplication project.

ViewController.swift

import UIKit
class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("viewDidLoad");
    }
}

When I start the application, viewDidLoad is called.

My scenario:
- press Home button (applicationDidEnterBackground)
- recall application (applicationWillEnterForeground)
and viewDidLoad is not called.

Is there another func to override?

Upvotes: 22

Views: 67537

Answers (6)

iDev
iDev

Reputation: 1042

If you want to call viewWillAppear in swift use this.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated) // No need for semicolon
}

Upvotes: 58

shubham bhagat
shubham bhagat

Reputation: 19

If you are using page view controllers and swiping between the controllers or if you are coming back from background, your viewDidLoad does not get called which means your viewDidLoad will get called only once. You need to set the data setup in viewWillAppear() since it gets called every time the view appears.

Upvotes: 1

Cristian C.
Cristian C.

Reputation: 302

Based on Noah response:
on ViewController.swift add refresh function and call it from AppDelegate.swift > applicationWillEnterForeground

ViewController.swift  

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        println("viewDidLoad");
        refresh();
    }

    func refresh(){
        println("refresh");
    }
}

.

AppDelegate.swift
func applicationWillEnterForeground(application: UIApplication!) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    ViewController().refresh();
}

Output:

viewDidLoad  
refresh  
refresh  
refresh  
refresh  

Upvotes: 4

massimobio
massimobio

Reputation: 818

Same as @Sunkas but in Swift 4:

open override func viewDidLoad()
{
    super.viewDidLoad()
    NotificationCenter.default.addObserver(self, selector: #selector(applicationWillEnterForeground(notification:)), name: .UIApplicationWillEnterForeground, object: nil)
    NotificationCenter.default.addObserver(self, selector: #selector(applicationDidEnterBackground(notification:)), name: .UIApplicationDidEnterBackground, object: nil)
}

deinit {
    NotificationCenter.default.removeObserver(self)
}

@objc private func applicationWillEnterForeground(notification: NSNotification) {
    print("will enter foreground")
}

@objc private func applicationDidEnterBackground(notification: NSNotification) {
    print("did enter background")
}

Upvotes: 2

Sunkas
Sunkas

Reputation: 9590

The best practice is to register for UIApplicationWillEnterForegroundNotification and UIApplicationWillEnterBackgroundNotification in your ViewController

public override func viewDidLoad()
{
    super.viewDidLoad()

    NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationWillEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
    NSNotificationCenter.defaultCenter().addObserver(self, selector: "applicationWillEnterBackground:", name: UIApplicationDidEnterBackgroundNotification, object: nil)
}

deinit {
    NSNotificationCenter.defaultCenter().removeObserver(self)
}

func applicationWillEnterForeground(notification: NSNotification) {
    println("did enter foreground")
}

func applicationWillEnterBackground(notification: NSNotification) {
    println("did enter background")
}

Upvotes: 8

Noah Witherspoon
Noah Witherspoon

Reputation: 57149

viewDidLoad is only called when your view controller’s view is first loaded—it remains in memory after that, so in general it will never be called again until you create another instance of the view controller. If you need to refresh content in your view controller when the application enters the foreground, you should create a method to do that and call it from applicationWillEnterForeground.

Upvotes: 2

Related Questions