2013-03-10 44 views
0

我有一个标签,用户将设置该号码后,该用户点击按钮的号码将被存储在数字variable.in appdelegate.m我想访问已设置的号码。 例如标签的输入是9:25 这里是我所做的。我认为在appdelegate.m中声明我的viewcontroller是错误的,但我不知道该怎么做。退出程序(后台输入)后可以维护变量吗?

ShowOfNotificationViewController.h

@property(strong,nonatomic) NSString *numbers; 

ShowOfNotificationViewController.m

@implementation ShowOfNotificationViewController 
@synthesize numbers; 

- (IBAction)setTime:(id)sender { 
numbers=TimeLabel.text; 
} 

ShowOfNotificationAppDelegate.m

#import "ShowOfNotificationAppDelegate.h" 
#import "ShowOfNotificationViewController.h" 

    - (void)applicationDidEnterBackground:(UIApplication *)application 
    { 
     ShowOfNotificationViewController *m; 

     NSNumberFormatter * f = [[NSNumberFormatter alloc] init]; 
     [f setNumberStyle:NSNumberFormatterDecimalStyle]; 
     NSArray *listItems = [m.numbers componentsSeparatedByString:@":"]; 
     NSNumber * myNumber1 = [f numberFromString:listItems[0]]; 
     NSNumber * myNumber2 = [f numberFromString:listItems[1]]; 
     NSLog(@"%@",myNumber1); 
     NSLog(@"%@",myNumber2); 
    } 

输出为NULL,NULL

+2

<在下面插入关于NSUserDefaults的回答> – CodaFi 2013-03-10 12:03:17

+0

抱歉,我不明白你的意思 – Nickool 2013-03-10 12:03:45

+0

让我看看NSNumberFormatter的初始化f – CodaFi 2013-03-10 12:13:19

回答

3
<!-- @CodaFi: here you are --> 

如果不是敏感数据,可以将该值存储在由操作系统保存在磁盘上的用户默认设置:

[[NSUserDefaults standardUserDefaults] setObject:self.numbers forKey:@"Numbers"]; 
[[NSUserDefaults standardUserDefaults] synchronize]; 

,并检索它:

self.numbers = [[NSUserDefaults standardUserDefaults] objectForKey:@"Numbers"]; 
+0

我无法从appdelegate中的self中访问数字,这里应该插入代码?它放在controller.m上吗? – Nickool 2013-03-10 12:25:09

+0

@nikparsa你决定。无论哪里都有道理。 – 2013-03-10 12:28:17

+0

非常感谢你我很懂 – Nickool 2013-03-10 12:38:51

0

人,你应该分配init你的ShowOfNotificationViewController并将其存储到一个属性或一些类ShowOfNotificationViewController * var ...

你不能希望简单的ShowOfNotificationViewController * m;将允许你的应用程序知道你想获得一个特定的元素....

你只是定义一个变种,但你没有分配它......程序应该怎么做?

试着想想: m应该是一个人,但他不在这里!他在哪里?谁知道。 当然,当你说m时,你叫什么名字,没有人会回答你。 (null)

但是如果你说m = giovanni; 然后你问他的名字,他会回答“乔瓦尼”!

你需要做的是 ShowOfNotificationViewController * m =以前创建的一些ShowOfNotificationViewController;

这将工作。

+0

我不是man.yes你是对的,但我不知道如何设置然后使用它 – Nickool 2013-03-10 12:28:20

+0

这很容易。在你的appDelegate.h中创建一个属性,alloc,在你的应用程序中初始化它,然后在你的did前面输入foreground,并使用self.yourVar引用它。这是一种方式。 – 2013-03-10 14:22:05

相关问题