Search code examples
iosios7nsdate

iOS7 - Storing NSDate value from button press to use later


I need to capture the result of NSDate when a button is pressed, so I can use it later on in the app. What is the best way / code to do this?

Update

Sorry for not including more information. New to this site, and wasn't aware of protocol.

So I understand how to respond to a button press. However, whilst I have written the result of NSDate to the timeStampLabel, what I really want to do is save the result of NSDate at that point to a key? or similar in order that I can use it later. I require the data to be accessible to a different class, however, have no need for the data to persist if the app was closed entirely (i.e. swiped up and off the screen).

The code I currently have is:

- (IBAction)tripButton:(id)sender {
    NSDate *date = [NSDate date];
    self.timeStampLabel.text = [NSString stringWithFormat:@"%@ ", date];
}

Solution

  • So, there are three methods (that I know of) to deal with passing information across different view controllers in iOS.

    • Lindsey Scott's answer suggests storing the information as properties on the App Delegate and reaching into the app delegate to set/retrieve the data.
    • Duncan C's answer explains my preferred method, using singletons.

    The third method that I know of is passing the information directly between the views that need it. This is simple to manage if you just need to pass the data from one view that is presented immediately before the view you're passing the information to.

    Assuming you're using segues, you'd do something like this...

    Given ViewController1 and ViewController2, give ViewController2 an NSDate public property to store the date you're passing to it. We'll call it passedDate in this example. We'll also give ViewController1 a property (private, probably) to store the date temporarily, we'll call it dateToPass in this example. Both properties should be strong.

    Now, in ViewController1, the IBAction that handles the button press will look like this:

    -(IBAction)buttonPressed:(id)sender {
        self.dateToPass = [NSDate date];
        [self performSegueWithIdentifier:@"segueMySegue" sender: self];
    }
    

    So now the button press saves the date to a property and calls the segue. As a note, we don't have to do both actions in this single button press. The main thing that's important is saving the date on the button press to a property so you can access it later. You can perform the segue however/whenever you want.

    Now, to actually pass the data, we're going to override the prepareForSegue: method:

    -(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
        if ([[segue identifier] isEqualToString:@"segueMySegue"]) {
            ViewController2 *dest = [segue destinationViewController];
            dest.passedDate = self.dateToPass;
        }
    }
    

    Now we've set the property passedDate on the next view controller to point to the dateToPass date object we set on the button press. We can access this property in ViewController2 and do with it whatever we need to do.