Swift - func viewWillAppear
Asked Answered
S

6

22

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?

Spicy answered 19/8, 2014 at 20:8 Comment(0)
S
58

If you want to call viewWillAppear in swift use this.

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated) // No need for semicolon
}
Strongarm answered 28/10, 2014 at 12:54 Comment(3)
I am curious: can you explain why we need to pass false?Minny
Don't pass false. Pass on the value of "animated" to the super class.Neogothic
old habits die hard (';') :)Bookplate
R
8

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")
}
Redness answered 29/4, 2015 at 11:11 Comment(0)
S
4

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  
Spicy answered 19/8, 2014 at 20:33 Comment(1)
Note that ViewController().refresh() creates a new instance of Viewcontroller. Also, since this is not stored anywhere ARC will deallocate it quite immediately.Redness
S
2

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.

Saponify answered 19/8, 2014 at 20:10 Comment(0)
C
2

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")
}
Chilli answered 22/10, 2017 at 17:38 Comment(1)
Not sure in which version it got changed but I had to change name: .UIApplicationWillEnterForeground to name: UIApplication.willEnterForegroundNotificationHawkie
P
1

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.

Purchase answered 30/8, 2020 at 19:36 Comment(1)
viewWillAppear does not get called for me on iOS 15 simulator when an app gets in background, then back into foregroundHawkie

© 2022 - 2024 — McMap. All rights reserved.