0

データを渡すだけではうまくいきません...AppDelegateの関数が呼び出されたときに、ViewControllerから文字列データをプルする必要があります。コードは次のとおりです。

アプリデリゲート:

- (void)applicationDidEnterBackground:(UIApplication *)application
{

AMViewController *viewController = [[AMViewController alloc] initWithNibName:@"AMViewController" bundle:nil];
self.xpData = viewController.xpLabel.text;
NSLog(@"Value of xpString in AD: %@", self.xpData);

}

私のViewControllerでは、文字列を渡す/取得するためのアクションを使用していません。基本的に、ユーザーがホームボタンを押したときにViewControllerからプルします。終了時にデータを保存したいので、これを行っています。ありがとう!

4

2 に答える 2

0

NSNotificationCenterを使用してみましたか

または共有デリゲート:

于 2013-01-13T04:35:00.440 に答える
0

あなたのAppDelegate.h

@property (atomic, copy) NSString *yourString;

変更した場所xpLabel.textで、これを行います

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
appDelegate.yourString = xpLabel.text;

- (void)applicationDidEnterBackground:(UIApplication *)application
{
    NSLog(@"Value of xpString in AD: %@", self.yourString);
}

別の方法

AMViewControllerこのようにクラスに値を設定します

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:xpLabel.text forKey:@"yourkey"];
[prefs synchronize];

applicationDidEnterBackgroundこのように値を取得します

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
yourString = [prefs stringForKey:@"yourkey"];
于 2013-01-13T12:00:49.157 に答える