首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Swift - func viewWillAppear

Swift - func viewWillAppear
EN

Stack Overflow用户
提问于 2014-08-19 20:08:39
回答 6查看 64.3K关注 0票数 22

我有一个标准的SingleViewApplication项目。

ViewController.swift

代码语言:javascript
复制
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。

我的场景:

  • 按主页按钮(applicationDidEnterBackground)
  • 召回应用程序(applicationWillEnterForeground) viewDidLoad也没有被调用。

还有别的功能可以覆盖吗?

EN

回答 6

Stack Overflow用户

发布于 2014-10-28 12:54:34

如果您想快速调用viewWillAppear,请使用此命令。

代码语言:javascript
复制
override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated) // No need for semicolon
}
票数 58
EN

Stack Overflow用户

发布于 2015-04-29 11:11:36

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

代码语言:javascript
复制
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")
}
票数 8
EN

Stack Overflow用户

发布于 2014-08-19 20:33:28

根据诺亚的答复:

在ViewController.swift上添加刷新函数并从AppDelegate.swift > applicationWillEnterForeground调用它

代码语言:javascript
复制
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");
    }
}

代码语言:javascript
复制
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();
}

输出:

代码语言:javascript
复制
viewDidLoad  
refresh  
refresh  
refresh  
refresh  
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25392124

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档