每次打开App并按下按钮时重新加载数据

时间:2016-12-16 21:03:43

标签: ios swift

每次从背景和按钮打开我的应用程序时,如何使用此功能?

Swift 3

tableView.reloadData() 

非常感谢你!

1 个答案:

答案 0 :(得分:4)

抓住前景通知:

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        NSNotificationCenter.defaultCenter().addObserver(self, selector: "willEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
    }

    func willEnterForeground(notification: NSNotification!) {
        // do whatever you want when the app is brought back to the foreground
        self.tableView.reloadData() 
    }

    deinit {
        // make sure to remove the observer when this view controller is dismissed/deallocated

        NSNotificationCenter.defaultCenter().removeObserver(self, name: nil, object: nil)
    }
}

重新加载您的桌面视图:

override func viewDidLoad() {
    super.viewDidLoad()

    self.tableview.reloadData()
}