4

I have an app with a UINavigationController with a tabBarController and quite a few views.

My two main views, that correspond to the two tabs, both show a nice MBProgressHUD when they are loading data. This is triggered from viewDidLoad and only intended to be visible once. Subsequent refreshes from the server do not use MBProgressHUD.

The problem is, viewDidLoad is being called again in certain situations. I believe this is because the view is unloaded because of memory constraints and then reloaded later, which triggers the code I only want to run on the first time.

My question: how can I ensure this is only called the first time they load the view, without trying to store a temporary count somewhere. All of my solutions seem horribly hacky. :)

Thanks!

clifgriffin
  • 2,008
  • 3
  • 21
  • 41

2 Answers2

2

in view controller:

@property (nonatomic, assign) BOOL loadStuff;

in init:

self.loadStuff = YES;

in viewDidLoad:

if (self.loadStuff) {
  // load stuff here
}

self.loadStuff = NO;
Terry Wilcox
  • 9,010
  • 1
  • 34
  • 36
  • See, now that was easy. Not sure why that didn't occur to me...was trying to make it more complicated than necessary. One note: because I setup the view with IB, initWithStyle wasn't being called. I leveraged the fact that BOOLs default to NO and just did my logic off of that. Thanks again. – clifgriffin Apr 09 '11 at 16:38
0

Only put up the HUD if you are actually exercising the code that takes a while. Then things will just work. Maybe something like:

@interface MyViewController : UIViewController {
    NSData* data;
}
@end

@implementation MyViewController
- (void)loadData {
    // put up HUD
    // start loading the data, will call loadDataFinished when done
}

- (void)loadDataFinished {
    // bring down HUD
}

- (void)viewDidLoad {
    [super viewDidLoad];
    if (data == nil) {
        [self loadData];
    }
}

@end
Daniel T.
  • 32,821
  • 6
  • 50
  • 72