简体   繁体   English

当我在tabBarController中的选项卡之间切换时,将调用哪个方法?

[英]which method will be called when i switch between tabs in tabBarController?

which method will be called when i switch between tabs in tabBarController 当我在tabBarController中的选项卡之间切换时,将调用哪个方法

i know at first time it will call viewDidLoad method ,i want to know is there any method that come in action when i switch to a particular tab (second time or third time ) . 我知道它第一次会调用viewDidLoad方法,我想知道当我切换到特定选项卡时(第二次或第三次)有任何方法可以实现。

regards 问候

You can use the UITabBarControllerDelegate method tabBarController:didSelectViewController: : 您可以使用UITabBarControllerDelegate方法tabBarController:didSelectViewController: ::

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController {
    //do something
}

This is pretty old, but it does come up on Google and is linked to from another answer. 这是相当古老的,但它确实出现在谷歌上,并与另一个答案相关联。 So, I thought I'd update it. 所以,我以为我会更新它。

If your UITableBarController is displaying a UIViewController (ie its view) then you have to check the ViewController methods that fire when a view disappears and appears. 如果您的UITableBarController正在显示UIViewController(即它的视图),那么您必须检查在视图消失并出现时触发的ViewController方法。 You could use viewWillDisappear to find out if your view is about to be switched away from, and viewWillAppear to test if your view just got switched back to. 您可以使用viewWillDisappear查看您的视图是否即将被切换,并查看viewWillAppear以测试您的视图是否已切换回。 Notice, the TabBarController typically keeps the ViewControllers loaded, just their views are moved out and in. The problem with using the TabBarDelegate method is that you need to know the name of your viewController, which makes that a dependency. 请注意,TabBarController通常会保持ViewControllers的加载,只是将它们的视图移出和移入。使用TabBarDelegate方法的问题是您需要知道viewController的名称,这使得它成为依赖项。 Change the name and it will probably break with xcode's poor ability to rename Class String representations. 更改名称,它可能会破坏xcode重命名类字符串表示的能力。 Avoid it. 躲开它。 Besides you don't want a bunch of conditional junk checking to see if your tabbar just loaded a particular tab unless you cannot avoid it. 此外,您不希望进行一系列条件垃圾检查,看看您的tabbar是否只是加载了一个特定的选项卡,除非你无法避免它。 The other thing to notice is that if a particular tab presents a TableViewController you may have to resort to other techniques if you need data in the cells to change in response to being switched away from. 另一件需要注意的事情是,如果某个特定选项卡显示TableViewController,如果您需要更改单元格中的数据以响应切换,则可能需要使用其他技术。 I'm using willMoveToWindow:(UIWindow *)newWindow to get notified in the UITableViewCell case when the view goes away. 我正在使用willMoveToWindow:(UIWindow *)newWindow在视图消失时在UITableViewCell案例中得到通知。 There's probably a better way. 可能有更好的方法。

The method viewWillDisappear: is triggered each time you are about to leave the current view controller (and hence the current tab) and viewWillAppear: is triggered each time a view is about to be displayed. 方法viewWillDisappear:在每次要离开当前视图控制器(因此当前选项卡)时触发,并且每次要显示视图时都会触发viewWillAppear: .

A full reference for these methods can be found in the UIViewController docs. 可以在UIViewController文档中找到这些方法的完整参考。

暂无
暂无

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

相关问题 在 TabBarController 中的选项卡之间切换/导航 - Switch/Navigate between Tabs in TabBarController TabBarController setSelectedIndex-将调用哪个委托方法 - TabBarController setSelectedIndex - Which delegate method will be called 在 Swift 中 TabBarController 的选项卡之间共享数据/模型时的最佳实践 - Best practice when sharing data/models between tabs of a TabBarController in Swift 在tabbarcontroller项目之间切换时,viewwillappear不会被称为swift 3 - When switching between tabbarcontroller items viewwillappear is not called swift 3 当我在TabBarController中切换视图时,ScrollView停止滚动,如何解决? - When i switch View In TabBarController the ScrollView Stop Scroll,How to Solve it? 当我从导航控制器(嵌入TabbarController)推送到新的TabbarController时,有短于正常 - When I from a navigation controller( which is embed in a TabbarController) push to a new TabbarController, there is short than normal 在TabBarController和ViewController之间切换的最佳方法 - Best approach to switch between TabBarController and ViewController tabBarController:shouldSelectViewController:设置委托时不调用 - tabBarController:shouldSelectViewController: not being called when delegate is set 用户点击选项卡栏时调用的方法。 我需要刷新标签内容 - What method is called when a user taps on a tab bar. I need to refresh tabs content 如何在不同选项卡下的导航控制器之间切换? - How do I switch between navigation controllers under different tabs?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM