ios Swift - func viewWillAppear

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25392124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 01:56:14  来源:igfitidea点击:

Swift - func viewWillAppear

iosswiftviewwillappear

提问by Cristian C.

I have a standard SingleViewApplicationproject.

我有一个标准SingleViewApplication项目。

ViewController.swift

视图控制器.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.

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

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

我的情况:
- 按主页按钮 ( applicationDidEnterBackground)
- 调用应用程序 ( applicationWillEnterForeground)
并且viewDidLoad没有调用。

Is there another func to override?

是否有另一个 func 可以覆盖?

回答by iDev

If you want to call viewWillAppearin swift use this.

如果您想viewWillAppear快速调用,请使用它。

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

回答by Sunkas

The best practice is to register for UIApplicationWillEnterForegroundNotificationand UIApplicationWillEnterBackgroundNotificationin your ViewController

最佳实践是注册UIApplicationWillEnterForegroundNotificationUIApplicationWillEnterBackgroundNotification在您的 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")
}

回答by Cristian C.

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

基于 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();
}

Output:

输出:

viewDidLoad  
refresh  
refresh  
refresh  
refresh  

回答by Noah Witherspoon

viewDidLoadis 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.

viewDidLoad仅在您的视图控制器的视图首次加载时调用 - 之后它会保留在内存中,因此通常在您创建视图控制器的另一个实例之前永远不会再次调用它。如果您需要在应用程序进入前台时刷新视图控制器中的内容,您应该创建一个方法来执行此操作并从applicationWillEnterForeground.

回答by massimobio

Same as @Sunkas but in Swift 4:

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