Swift - func viewWillAppear

22

我有一个标准的SingleViewApplication项目。

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");
    }
}

当我启动应用程序时,会调用viewDidLoad函数。

我的情况是:
- 按下Home键(applicationDidEnterBackground
- 重新调用应用程序(applicationWillEnterForeground
但是viewDidLoad 没有被调用。

有没有其他函数可以重写?

6个回答

58

如果您想在Swift中调用viewWillAppear,请使用以下方法。

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

3
好的,我可以为您进行翻译。对于您的问题,“为什么我们需要传递false?” 我能理解您的好奇心。简单来说,这是因为在特定情况下,通过将false传递给函数或方法,可以防止其执行某些操作或采取某些默认行为。 - Camillo
不要传递 false。将“animated”的值传递给超类。 - Bocaxica
7
老习惯难改。(';') :) - Macistador

8

最佳实践是在您的ViewController中注册UIApplicationWillEnterForegroundNotificationUIApplicationWillEnterBackgroundNotification

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")
}

4

根据Noah的回应:
在ViewController.swift中添加刷新函数,并从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();
}

输出:

viewDidLoad  
refresh  
refresh  
refresh  
refresh  

3
请注意,ViewController().refresh() 创建了一个新的 ViewController 实例。由于该实例没有被存储在任何地方,因此 ARC 会立即对其进行释放。 - Sunkas

2

与 @Sunkas 相同,但在 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")
}

不确定在哪个版本中更改了,但我不得不将 name: .UIApplicationWillEnterForeground 更改为 name: UIApplication.willEnterForegroundNotification - sergeyski.com

2

viewDidLoad 只有在你的视图控制器的视图首次加载时才会被调用——它在此后仍然保留在内存中,因此通常情况下除非你创建另一个视图控制器实例,否则不会再次调用它。如果你需要在应用程序进入前台时刷新视图控制器中的内容,则应创建一个方法来执行该操作,并从 applicationWillEnterForeground 中调用它。


1
如果您正在使用页面视图控制器并在控制器之间滑动,或者如果您从后台返回,则不会调用viewDidLoad,这意味着viewDidLoad只会被调用一次。您需要在viewWillAppear()中设置数据设置,因为它每次显示视图时都会被调用。

当应用程序进入后台,然后回到前台时,在iOS 15模拟器上我的viewWillAppear方法没有被调用。 - sergeyski.com

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接