Swift - func viewWillAppear

时间:2014-08-19 20:08:39

标签: ios swift viewwillappear

我有一个标准的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 个答案:

答案 0 :(得分:49)

如果您想快速拨打viewWillAppear,请使用此功能。

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

答案 1 :(得分:6)

最佳做法是在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")
}

答案 2 :(得分:5)

基于诺亚回应:
在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 :(得分:2)

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

答案 4 :(得分:1)

与@Sunkas相同,但在Swift 4中:

    @Bean
    public FilterRegistrationBean jwtFilter() {
        final FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(new AuthenticationFilter());
        registrationBean.addUrlPatterns("/secure/*");
        registrationBean.setOrder(1);

        return registrationBean;
    }

答案 5 :(得分:0)

如果您使用的是页面视图控制器并在控制器之间滑动,或者如果您是从后台回来的,则SearchResult result = cloudinary.Search() .Expression("tags=shoe AND women AND red") .WithField("tags") .Execute(); 不会被调用,这意味着viewDidLoad只会被调用一次。您需要在viewDidLoad中设置数据设置,因为每次视图出现时都会被调用。