2013-03-10 63 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

相關問題