Monday, 19 August 2013

viewDidLoad not working

viewDidLoad not working

So in a View Controller I have a save method to save a string to a .txt as
shown below
-(void)save: (NSString *)number
{
NSError *error = nil;
NSString *documents = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [NSString
stringWithFormat:@"%@/numberToSave.txt", documents];
NSMutableString *numba = [[NSMutableString alloc]init];
[numba setString:number];
[numba writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:&error];
}
Then, in my main view controller I have a loading method to retrieve the
number and set the label text to the number saved.
-(void)loadNumber
{
NSString *documents = [NSHomeDirectory()
stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [NSString
stringWithFormat:@"%@/numberToSave.txt", documents];
NSError *error = nil;
NSString *numba =[[NSString alloc] initWithContentsOfFile:filePath
encoding:NSASCIIStringEncoding
error:&error];
self.numberText.text = numba;
}
Still in the main view controller, I have tried these two things
-(void)viewDidLoad
{
[super viewDidLoad];
[self loadNumber];
}
And
-(void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:YES];
[self loadNumber];
}
Neither of these gets the view controller to update when I navigate back
to it. This is what happens:
I navigate to the secondary view controller
I choose and click a cell from the tableView on that viewController
It appends the string correctly- I checked with NSLog
I go back to main view controller
Label hasn't updated
I go back to the secondary view controller
Choose the same cell again
Doesn't append string - Again using NSLog
Go Back to main controller
Label updates to the appended string
Any help would be greatly appreciated... Thanks!!

No comments:

Post a Comment