简体   繁体   English

TableView的方法,仅在第一次显示视图时才调用

[英]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 另一个选择是使用方法viewDidLoadviewDidAppear

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何仅在第一次显示视图控制器时在viewDidAppear中触发动作? - How to trigger an action in viewDidAppear only the first time a view controller is shown? 首次显示视图时,UIViewController中的哪个方法仅被调用一次? viewDidLoad? - Which method(s) in UIViewController is called only once, when the view is first presented? viewDidLoad? Swift:仅调用一个TableView方法 - Swift: Only one TableView Method is called 第一次显示视图之前的scrollToItemAtIndexPath - scrollToItemAtIndexPath before the view is shown the first time 每次显示视图时运行方法 - iOS - Run method every time view is shown - iOS 奇怪的UIViewController hidesBottomBarWhenPushed布局错误,仅在第一次按下视图控制器时发生 - Weird UIViewController hidesBottomBarWhenPushed layout bug that only occurs the very first time a view controller is pushed tableView:cellForRowAtIndexPath内部的函数:仅在前两个单元格中调用 - function inside tableView:cellForRowAtIndexPath: only called for the first 2 cell 循环仅在第一次调用时运行一次,无限次运行 - Loop runs only once first time it's called, infinite second time didSelectRowAtIndexPath仅在长按嵌入容器视图中的tableView时才调用 - didSelectRowAtIndexPath called only with long press on a tableView embedded in a container view 仅在选择第二个单元格后才调用TableView委托方法 - TableView delegate method called only after a second cell is selected
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM