1

我在 xcode 4.3.1 中制作了一个简单的单视图程序。我希望视图根据它是在应用程序第一次启动时加载还是在恢复时执行不同的操作。

谁能告诉我最好的方法来做到这一点?

appDelegate 没有对我的 viewController 的引用,所以我不确定我是否可以从我的 AppDelegate didFinishLaunchingWithOptions 方法中传递一个变量。

当 ViewController 似乎没有在任何地方实例化时,AppDelegate 如何与 ViewController 通信?

谢谢!

4

2 回答 2

1

可以使用 NSNotificationCenter 知道应用程序何时进入前台,然后可以注册个别 VC 来关心该事件。例如:

- (void)loadView {
[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(applicationWillEnterForeground)
                                             name:UIApplicationWillEnterForegroundNotification
                                           object:nil];
}

VC 在 loadView(或任何其他方法)期间注册。然后当app进入前台时,方法

- (void)applicationWillEnterForeground;

叫做。只要记住在 dealloc 或 viewDidUnload 中取消注册。

- (void)dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
于 2012-06-22T01:49:27.200 回答
0

您可以使用 [application:didFinishLaunchingWithOptions:] 来确定您是否刚刚开始。它仅在您启动时调用一次。您可以将其与设置一些标志和 [applicationWillEnterForeground:(UIApplication *)application] 相结合,以确定您是启动还是只是返回前台。

于 2012-06-22T00:10:21.780 回答