[英]A simple viewDidLoad question
I have an app with a UINavigationController
with a tabBarController
and quite a few views. 我有一个带有
UINavigationController
的应用程序,其中带有tabBarController
和很多视图。
My two main views, that correspond to the two tabs, both show a nice MBProgressHUD
when they are loading data. 与这两个选项卡相对应的两个主视图在加载数据时均显示一个漂亮的
MBProgressHUD
。 This is triggered from viewDidLoad
and only intended to be visible once. 这是从
viewDidLoad
触发的,并且只能显示一次。 Subsequent refreshes from the server do not use MBProgressHUD
. 服务器随后的刷新不使用
MBProgressHUD
。
The problem is, viewDidLoad
is being called again in certain situations. 问题是,在某些情况下会再次调用
viewDidLoad
。 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: 在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. 仅当您实际上在执行需要一段时间的代码时,才安装HUD。 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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.