2013-01-13 24 views
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拉它。我这样做是因为我想在退出时保存数据。谢谢!

+0

当应用程序进入后台你要创建一个视图控制器的价值? xpLabel.text如何设置?我猜这不是。 – Bejmax

+0

你用你写的代码在你的日志上得到什么?它是空的还是比你想要的还要有价值? – ashiina

+0

'AMViewController * viewController = [[AMViewController alloc] initWithNibName:@“AMViewController”bundle:nil];'这将创建一个新的视图控制器。你需要的是现有的视图控制器。你是如何创建这个视图控制器?它是rootviewcontroller并在didFinidhLaunching方法中创建? – iDev

回答

0

在你AppDelegate.h

@property (atomic, copy) NSString *yourString; 

如果你已经改变xpLabel.text,在

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

做到这一点

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

现在,另一种方法

设置你的AMViewController类这样

0123值

让您在applicationDidEnterBackground这样

NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults]; 
yourString = [prefs stringForKey:@"yourkey"]; 
+0

非常感谢你,解决了这个问题! :) –

相关问题