[英]A method of TableView that's only called the very first time the view is shown
I have a master view (UITableView) with two properties that I want to be set only the first time the view is shown (that is; only when the application starts), but not when the view is shown again later on runtime (eg when a push-segue drags it on again etc.) 我有一个主视图(UITableView),它具有两个属性,我只想在第一次显示视图时(即,仅在应用程序启动时)设置,而在稍后在运行时再次显示视图时(例如,当推送搜索将其再次拖动等)
How is this possible? 这怎么可能? Maybe there's something I've missed? 也许我错过了什么?
Thanks in advance. 提前致谢。 All help is greatly appreciated. 非常感谢所有帮助。
You may use dispatch_once
in a corresponding view controller's method viewDidAppear:
: 您可以在相应的视图控制器的方法viewDidAppear:
使用dispatch_once
:
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
static dispatch_once_t once;
dispatch_once(&once, ^{
... your code to be executed only once ...
});
}
Set it in the init
method of the class. 在类的init
方法中进行设置。
Then, if the class is shown again, viewWillAppear
will be called, so you can set the property back to the value you want. 然后,如果再次显示该类,则将调用viewWillAppear
,因此您可以将属性设置回所需的值。
Another option is to play with the methods viewDidLoad
and viewDidAppear
另一个选择是使用方法viewDidLoad
和viewDidAppear
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.