开发者

A simple viewDidLoad question

开发者 https://www.devze.com 2023-02-24 00:11 出处:网络
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 dat

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 hor开发者_JAVA技巧ribly 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
0

精彩评论

暂无评论...
验证码 换一张
取 消