Nickool
Nickool

Reputation: 3702

is it possible to maintain variable after exiting program (background entering)?

I have a label which user will set the number after that user hits the button the number will be stored in numbers variable.in appdelegate.m I want to access the number which has been set. for example the input that label has is 9:25 here is what I have done.I think that it is wrong to declare my viewcontroller in appdelegate.m but I didn't know what to do else.

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);
    }

the output is null,null

Upvotes: 0

Views: 69

Answers (2)

Giuseppe Lanza
Giuseppe Lanza

Reputation: 3699

Man, you should alloc init your ShowOfNotificationViewController and store it into a property or some class ShowOfNotificationViewController * var...

You can't hope that the simple ShowOfNotificationViewController *m; will allow your application to know that you want to get a specific element....

You are just defining a var, but you are not assigning it... what should the program do with that?

Try to think about that: m should be a person, but he is not here! Where is he? who knows. Of course when you say m, what is your name, nobody will answer you. (null)

but if you say m = giovanni; and then you ask to m his name he will answer "giovanni"!

What you need to do is ShowOfNotificationViewController *m = some ShowOfNotificationViewController previously created;

this will work.

Upvotes: 0

user529758
user529758

Reputation:

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

If it is not sensitive data, you can store the value in the user defaults which is persisted to disk by the OS:

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

And to retrieve it:

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

Upvotes: 3

Related Questions