0
votes

I have this problem. I have a operation. But if i want to save? I want to save all (textfield data picker and result in a label)

I have 2 textfield and 1 datapicker + the result in a Label.

this is my M :

#import "ViewController.h"
#import "ResultViewController.h"



@interface ViewController ()

@end

@implementation ViewController

@synthesize myLabel;

-(IBAction)calculate:(id)sender{



    NSDate *past = _data.date ;
    NSDate *now = [NSDate date];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                     initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit;
    NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                        fromDate:past
                                                          toDate:now
                                                        options:0];
    int z = [components day];
    int a = ([_textField1.text intValue]);
    int b = a*([_textField2.text intValue]);

    int r = b * z / 20;

    myLabel.text = [[NSString alloc] initWithFormat:@"%d", r];



}

- (void)viewDidLoad
{
    [super viewDidLoad];



    NSDate *past = _data.date ;
    NSDate *now = [NSDate date];

    NSCalendar *gregorianCalendar = [[NSCalendar alloc]
                                     initWithCalendarIdentifier:NSGregorianCalendar];
    NSUInteger unitFlags = NSDayCalendarUnit;
    NSDateComponents *components = [gregorianCalendar components:unitFlags
                                                        fromDate:past
                                                          toDate:now
                                                         options:0];
    int z = [components day];
    int a = ([_textField1.text intValue]);
    int b = a*([_textField2.text intValue]);

    int r = b * z / 20;

    myLabel.text = [[NSString alloc] initWithFormat:@"%d", r];

    self.textField1.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key1"];
    self.textField2.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key2"];
    self.myLabel.text = [[NSUserDefaults standardUserDefaults] objectForKey:@"key3"];
    self.data = [[NSUserDefaults standardUserDefaults] objectForKey:@"key4"];


[[NSUserDefaults standardUserDefaults]synchronize];


    // Do any additional setup after loading the view, typically from a nib.
}

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {


    if ([segue.identifier isEqualToString:@"detail"]) {

        ResultViewController* destVC = (ResultViewController*)segue.destinationViewController;

        destVC.myString = self.myLabel.text;

    }
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:YES];



    [[NSUserDefaults standardUserDefaults] setObject:self.textField1.text forKey:@"key1"];
    [[NSUserDefaults standardUserDefaults] setObject:self.textField2.text forKey:@"key2"];
    [[NSUserDefaults standardUserDefaults] setObject:self.myLabel.text forKey:@"key3"];
    [[NSUserDefaults standardUserDefaults] setObject:self.data forKey:@"key4"];

    [[NSUserDefaults standardUserDefaults]synchronize];

}
@end

This code is Right?

Thank you....

2
On first glance the code looks right. What specifically is your problem? - Ryan Poolos
But now my save don't function! I have 3 viewcotroller. I have this operation in 3rd viewcontroller. But if I go to first my date lost. And if i go only in 2nd view controller my date are saving. - Federico Ciabatta

2 Answers

1
votes

Couple things.

  • In your -viewDidLoad, you call -synchronize for some reason. All this does is perform the save function for any events on your user defaults. From what I can see, you don't have any and can skip this call to -synchronize.
  • One of your logs is showing the value of 'r'. The value is an int value and you should use '%i' to display this correctly. You are performing some calculations to get that value, so you may want that to be a double or something else, but you are currently casting it to an int.

I don't see anything regarding the user defaults that looks out of sorts. So maybe you can specify what exactly is wrong or isn't being done correctly to get some better help.

0
votes

Yes code looks right. What is your _data variable type? If you having trouble saving to NSUser. Try to save self.data.date

One tip : You don't need to synchronize after getting values from NSUserdefautls. Use synchronize after setting values to NSUserdefaults.