Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

goedsole

macrumors newbie
Original poster
Apr 21, 2010
9
0
Dear Y'all -

I have a tab bar app in which one view allows setting a date value and another view should display information (i.e. UILabel.text) derived from the selected date. Things work fine except I have to quit the app for the information to update based on changes in the setting. The information is updated within viewDidLoad; what I want is something like viewDidLoad for the display view controller except executed each time that view is visited. I tried viewWillAppear to no avail.

Code in display view is:

Code:
- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSString *filePath = [self dataFilePath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
		NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
		NSDate *soberDate = [array objectAtIndex:0];
		dayCount.text = [NSString stringWithFormat:@"%g", 
						 floor(([soberDate timeIntervalSinceNow] / (24*60*60)) * -1) + 2 ];
		[soberDate release];
		[array release];
	}	
	//	[filePath release];
}
 
Dear Y'all -

I have a tab bar app in which one view allows setting a date value and another view should display information (i.e. UILabel.text) derived from the selected date. Things work fine except I have to quit the app for the information to update based on changes in the setting. The information is updated within viewDidLoad; what I want is something like viewDidLoad for the display view controller except executed each time that view is visited. I tried viewWillAppear to no avail.

try viewdidshow instead...
 
viewdidshow?

Dear Y'all -

I don't see that there is a viewDidShow message in the API I have tried viewDidAppear which doesn't help.

- Billy
 
More code snippets

Dear Y'all -

I'm not sure what's useful to see, but there ain't much more than what I've included below that's related.

settingsViewController.h contains:

Code:
#import <UIKit/UIKit.h>

#define kFilename	@"settings.plist"

@interface settingsViewController : UIViewController {
	UIDatePicker	*soberDatePicker;
}
@property (nonatomic, retain) IBOutlet UIDatePicker *soberDatePicker;
-(IBAction)buttonPressed;
-(NSString *)dataFilePath;
-(void)applicationWillTerminate:(NSNotification *)notification;

@end

settingsViewController.m contains:

Code:
#import "settingsViewController.h"


@implementation settingsViewController 
@synthesize soberDatePicker;

-(NSString *)dataFilePath {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(
														 NSDocumentDirectory,
														 NSUserDomainMask, 
														 YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	return [documentsDirectory stringByAppendingPathComponent:kFilename];
}
-(void)applicationWillTerminate:(NSNotification *)notification {
	
/*	NSTimeInterval oldInterval = [[soberDatePicker date] timeIntervalSinceReferenceDate];
	double secondsSinceMidnight = fmod(oldInterval,86400.0);
	NSDate *myNewDate = [NSDate dateWithTimeIntervalSinceReferenceDate:(oldInterval - secondsSinceMidnight)];
	
	NSMutableArray *array = [[NSMutableArray alloc] init];
	[array addObject:myNewDate];
	[array writeToFile:[self dataFilePath] atomically:YES];
 */
//	[array release];
}

-(IBAction)buttonPressed {
	NSDate *selected = [soberDatePicker date];
	NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
	[dateFormat setDateFormat:@"MMMM d, yyyy"];
	NSString *message = [dateFormat stringFromDate:selected];
	UIAlertView *alert = [[UIAlertView alloc]
						  initWithTitle:@"Date and Time Selected\nRelaunch to Apply"
						  message:message 
						  delegate:nil 
						  cancelButtonTitle:@"OK" 
						  otherButtonTitles:nil];
	[alert show];
	
	NSTimeInterval oldInterval = [[soberDatePicker date] timeIntervalSinceReferenceDate];
	double secondsSinceMidnight = fmod(oldInterval,86400.0);
	NSDate *myNewDate = [NSDate dateWithTimeIntervalSinceReferenceDate:(oldInterval - secondsSinceMidnight)];
	NSMutableArray *array = [[NSMutableArray alloc] init];
	[array addObject:myNewDate];
	[array writeToFile:[self dataFilePath] atomically:YES];	
	
	[alert release];
//	[message release];
}

#pragma mark -
-(void)viewDidLoad {
	NSString *filePath = [self dataFilePath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
		NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath];
		[soberDatePicker setDate:[array objectAtIndex:0]];
		[array release];
	}
	UIApplication *app = [UIApplication sharedApplication];
	[[NSNotificationCenter defaultCenter] addObserver:self
											 selector:@selector(applicationWillTerminate:)
												 name:UIApplicationWillTerminateNotification
											   object:app];
	[super viewDidLoad];
}

[...]
@end

mainViewController.h contains:

Code:
#import <UIKit/UIKit.h>

#define kFilename	@"settings.plist"

@interface mainViewController : UIViewController {
	UILabel	*dayCount;
}

@property (nonatomic, retain) IBOutlet UILabel *dayCount;
-(NSString *)dataFilePath;

@end

and finally, mainViewController.m contains:

Code:
#import "mainViewController.h"

@implementation mainViewController 
@synthesize dayCount; 

-(NSString *)dataFilePath {
	NSArray *paths = NSSearchPathForDirectoriesInDomains(
														 NSDocumentDirectory,
														 NSUserDomainMask, 
														 YES);
	NSString *documentsDirectory = [paths objectAtIndex:0];
	return [documentsDirectory stringByAppendingPathComponent:kFilename];
}

- (void)viewDidLoad {
    [super viewDidLoad];
	
	NSString *filePath = [self dataFilePath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
		NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
		NSDate *soberDate = [array objectAtIndex:0];
		dayCount.text = [NSString stringWithFormat:@"%g", 
						 floor(([soberDate timeIntervalSinceNow] / (24*60*60)) * -1) + 2 ];
		[soberDate release];
		[array release];
	}	
	//	[filePath release];
}
- (void)viewDidAppear {
    [super viewDidAppear:NO];
	
	NSString *filePath = [self dataFilePath];
	if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
		NSArray *array = [[NSArray alloc] initWithContentsOfFile:filePath]; 
		NSDate *soberDate = [array objectAtIndex:0];
		dayCount.text = [NSString stringWithFormat:@"%g", 
						 floor(([soberDate timeIntervalSinceNow] / (24*60*60)) * -1) + 2 ];
		[soberDate release];
		[array release];
	}	
	//	[filePath release];
}

[...]

@end

When debugging bouncing around in the interface never reaches a breakpoint set in the code associated with viewDidAppear.

Thanks again for the help.

- Billy
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.