【问题标题】:Swift - func viewWillAppearSwift - 函数 viewWillAppear
【发布时间】:2014-08-19 20:08:39
【问题描述】:

我有一个标准的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。

我的场景:
- 按主页按钮 (applicationDidEnterBackground)
- 召回申请 (applicationWillEnterForeground)
并且viewDidLoad 没有被调用。

还有其他要覆盖的函数吗?

【问题讨论】:

    标签: ios swift viewwillappear


    【解决方案1】:

    如果你想快速调用viewWillAppear,请使用这个。

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

    【讨论】:

    • 我很好奇:你能解释一下为什么我们需要通过false吗?
    • 不要传错。将“animated”的值传递给超类。
    • 旧习难改 (';') :)
    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

      根据诺亚的回应:
      在 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  
      

      【讨论】:

      • 请注意,ViewController().refresh() 创建了一个新的 Viewcontroller 实例。此外,由于它没有存储在任何地方,ARC 将立即释放它。
      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

          【解决方案6】:

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-12-07
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多