Update and outlets value when visiting view

Discussion in 'iOS Programming' started by goedsole, Apr 22, 2010.

  1. goedsole macrumors newbie

    Joined:
    Apr 21, 2010
    #1
    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];
    }
    
     
  2. MrCrispy macrumors member

    MrCrispy

    Joined:
    Apr 10, 2008
    Location:
    Jacksonville, Florida
    #2
    try viewdidshow instead...
     
  3. goedsole thread starter macrumors newbie

    Joined:
    Apr 21, 2010
    #3
    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
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    Can you show us the code you used in viewDidAppear:, as well as anything that supports it (ivar definitions, etc.)?
     
  5. MrCrispy macrumors member

    MrCrispy

    Joined:
    Apr 10, 2008
    Location:
    Jacksonville, Florida
    #5
    gah, appear not show. This is what I get for replying while in a hurry to go to lunch.
     
  6. goedsole thread starter macrumors newbie

    Joined:
    Apr 21, 2010
    #6
    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
     
  7. Luke Redpath macrumors 6502a

    Joined:
    Nov 9, 2007
    Location:
    Colchester, UK
    #7
    Code:
    - (void)viewDidAppear {
    
    should be

    Code:
    - (void)viewDidAppear:(BOOL)animated {
    
     
  8. goedsole thread starter macrumors newbie

    Joined:
    Apr 21, 2010
    #8
    Yahoo!

    Dear Luke et al. -

    Thanks tons. That answer is going to save me a million heartaches as I go forward.

    - Billy
     

Share This Page