简体   繁体   中英

A simple viewDidLoad question

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!

in view controller:

@property (nonatomic, assign) BOOL loadStuff;

in init:

self.loadStuff = YES;

in viewDidLoad:

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

self.loadStuff = NO;

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM