简体   繁体   English

每次选择选项卡时执行一种方法

[英]perform a method every time a tab is selected

I have tabview based project. 我有基于tabview的项目。 how can I make a view connected to a tab perform a certain method every time the tab is selected? 每次选择选项卡时,如何使连接到选项卡的视图执行某种方法?

Please help, Thanks 请帮忙,谢谢

EDIT: I mistook TabBar for TableView. 编辑:我误认为TableView的TabBar。 I'm sorry for that. 对此我感到抱歉。 I let my old answer being here, it might help you in another problem. 我让我的老答案在这里,这可能会帮助您解决另一个问题。

The answer that bryamac gave you is the right one. bryamac给您的答案是正确的。

When you click the tab then you probably load some UIView to display some content. 当您单击选项卡时,您可能会加载一些UIView来显示一些内容。 For that UIView you must have a controller class. 对于该UIView,您必须具有一个控制器类。 In that controller class there are - (void)viewDidAppear:(BOOL)animated and - (void)viewWillAppear:(BOOL)animated methods which you may use. 在该控制器类中,可以使用- (void)viewDidAppear:(BOOL)animated- (void)viewWillAppear:(BOOL)animated方法。

Old answer 旧答案

What you want to do is make a class to a UITableViewDelegate. 您想要做的是为UITableViewDelegate创建一个类。

Then you must set this class as the delegate of the UITableView, you can to that in the Interface Builder by right-clicking on the UITableView and dragging its delegate to the class that you have chosen, eg its own controller class. 然后,必须将该类设置为UITableView的委托,然后可以通过在UITableView上单击鼠标右键并将其委托拖到所选的类(例如其自己的控制器类)中,以在Interface Builder中进行设置。

Then you implement this method inside the UITableViewDelegate-class: 然后,您可以在UITableViewDelegate-class内部实现此方法:

– tableView:didSelectRowAtIndexPath;

More information here . 更多信息在这里

Assuming the tab var is the main way to make the view appear, you can hook code into: 假定选项卡var是使视图出现的主要方法,则可以将代码挂钩:

- (void)viewDidAppear:(BOOL)animated

or ... 要么 ...

- (void)viewWillAppear:(BOOL)animated

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

相关问题 每次从标签栏按下标签时,如何执行方法? - How do I execute a method every time a tab is pressed from the tab bar? 每次选择Tableview(选项卡栏项目)时需要在何处放置代码 - Where to put code that needs to be executed every time a Tableview (tab bar item) is selected UITabBarController检查是否首次选择了选项卡 - UITabBarController check if tab selected for the first time iphone - 每次更改选项卡时如何调用 function? - iphone - How to call a function every time a tab is changed? 每次单击“更多”选项时,导航回到选项卡选项表 - Navigate back to table of tab options every time 'more' option is clicked 每次显示UIView时是否都调用一种方法? - Is there a method that was called every time a UIView was show? 每次选择选项卡时如何获得相同的视图? - How to get the same view each time when the tab is selected? Objective-C每天在给定时间执行方法-杂乱的实现 - Objective-C Perform method at given time daily - messy implementation 如何测量执行方法所需的时间? - How can I measure the time it takes to perform a method? 如何在选择器视图中每次选择/设置选定行作为默认选定行 - How to select/set a selected row in picker view as default selected row every time
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM