gpt4 book ai didi

ios - 进入 View 时更新特定的 ViewController

转载 作者:行者123 更新时间:2023-11-29 02:09:44 27 4
gpt4 key购买 nike

我有两个 UIViewController,每当它们出现时我都想更新它们。

In DashBordViewControlller I have therefore added this code:

override func viewDidLoad() {
super.viewDidLoad()

let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate! as AppDelegate
appDelegate.dashboardViewController = self

and in DetailViewController this code:

override func viewDidLoad() {
super.viewDidLoad()

let appDelegate:AppDelegate = UIApplication.sharedApplication().delegate! as AppDelegate
appDelegate.detailViewController = self

In AppDelegate I have added this code:

class AppDelegate: UIResponder, UIApplicationDelegate {

var window: UIWindow?

var dashboardViewController:DashboardViewController?
var detailViewController:DetailViewController?

func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
return true
}

func applicationWillResignActive(application: UIApplication) {
}

func applicationDidEnterBackground(application: UIApplication) {
println("Goodbye world")
}

func applicationWillEnterForeground(application: UIApplication) {
println("Hello again world")
dashboardViewController?.cleanCDandQueryHKLoop()
detailViewController?.rememberWhatSegmentIsPressed()
}

我的问题是,只要有一个 ViewController 进入 View ,两个 UIViewControllers 都会更新。例如,如果我关闭应用程序(主页)然后重新打开它。

我如何在 AppDelegate 中检查哪个 ViewController 进入 View 并只更新那个?任何帮助将不胜感激 - 谢谢!

最佳答案

您应该像这样在 View Controller 的 viewWillAppear 中添加一个观察者:

override func viewWillAppear(animated: Bool) {
super.viewWillAppear(animated)
NSNotificationCenter.defaultCenter().addObserver(self, selector: "willEnterForeground:", name: UIApplicationWillEnterForegroundNotification, object: nil)
}

之后,实现你的功能:

func willEnterForeground(notification: NSNotification!) {
// View Controller is brought to Foreground
}

哦,别忘了移除 viewWillDisappear 上的观察者:

override func viewWillDisappear(animated: Bool) {
super.viewWillDisappear(animated)
NSNotificationCenter.defaultCenter().removeObserver(self, name: nil, object: nil)
}

关于ios - 进入 View 时更新特定的 ViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29417010/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com