Question

I have a global variable in FirstViewController.h

extern NSString *dateString;

it records the date/time of when I press saveText button. There is another button called readText which pushes a UITableView. The first cell on this table will print the date saved.

However, this only works if I first press the saveText button. Otherwise, if I just press readText, it crashes. How can I get it so that if there is no current datetime saved, that it doesn't crash?

Here is where I wrote the timestamp for saveButton:

-(IBAction)saveText:(id)sender{

//code to save text, irrelevant to the question//



    dateString = [NSDateFormatter localizedStringFromDate:[NSDate date]
                                                          dateStyle:NSDateFormatterShortStyle
                                                          timeStyle:NSDateFormatterShortStyle];
    NSLog(@"%@",dateString);
}

and here is the code to load the tableviewcells with the timestamp

- (void)viewDidLoad
{
    [super viewDidLoad];




    NSString *str = @"Text- saved on: ";
    str = [str stringByAppendingString: dateString];    
    self.list = [[NSArray alloc] initWithObjects: str, nil];


}
Was it helpful?

Solution

In my methods that use the variable dateString,

I used:

if([dateString length] == 0){
 //perform standard procedure 
}
else {
   //what I want it to do 
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top