如何在AppDelegate和SecondViewController之间传递数据

时间:2012-09-12 21:18:13

标签: objective-c uiviewcontroller appdelegate pass-data

我有@property NSDate个实例,它在方法- (void)applicationWillResignActive:(UIApplication *)application 中有时间。

我已将ViewControllerSecondController连接到Storyboard所以在AppDelegate实现块中的代码中,我没有任何ViewController和SecondViewController实例。方法- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 仅返回YES。

问题是:我必须将时间从@property NSDate实例传递到SecondViewController,这会在应用程序生效时显示,但我不知道如何检查是否SecondViewController应用程序来自后台后显示。

我知道我可以使用AppDelegate:

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

然后得到他的NSDate @property,但我如何检查何时需要获取此数据?

我应该使用ViewController的哪种方法?

1 个答案:

答案 0 :(得分:1)

如果您希望SecondViewController的视图在显示时始终具有当前日期值,请在viewWillAppear:期间从数据模型(在本例中为AppDelegate)中获取它。