0

传递数据并不能解决问题......当调用 AppDelegate 中的函数时,我需要从视图控制器中提取字符串数据。这是代码:

在应用委托中:

- (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

在你的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 回答