我正在研究從網站獲取數據的應用程序。當用戶點擊主頁按鈕,然後再次打開應用程序(從後臺),我想重新加載數據到viewController。如何重新加載功能,如果你打回家,然後再次打開應用程序?
我嘗試下面的代碼:
在應用程序委託:
class AppDelegate: UIResponder, UIApplicationDelegate {
var myViewController: ViewController?
---------
var myViewController: rootViewController?
func applicationDidEnterBackground(application: UIApplication) {
print("Goodbye world") //.... then whatever code after pressing the home button
}
func applicationWillEnterForeground(application: UIApplication) {
print("Hello World")
myViewController.ObtianData() // which is pretty much the func in my app that fetch data from the web and display it in tableView
}
然後在視圖控制器viewDidLoad中
override func viewDidLoad() {
// I added the print to log here to check if the viewDidLoad function is being called but apparently it is not.
print ("Hello again from ViewController")
let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate! as! AppDelegate
appDelegate.myViewController? = self
}
任何建議下?
你有沒有嘗試過的代碼移入viewWillAppear中? – WMios
是的,它也沒有爲我工作 – abha