简体   繁体   English

标签栏控制器 - 如何重新选择选定的标签

[英]Tab Bar Controller - how to reselect a selected tab

I started with the default Tabbed Application, added some tabs in the Storyboards with their own viewcontrollers, how can I know when a tab that's already selected, get's touched again?我从默认的选项卡应用程序开始,在故事板中添加了一些带有自己的视图控制器的选项卡,我怎么知道什么时候已经选择了一个选项卡,再次被触摸?

Tab 1 goes to a webview that has loaded other pages, when the user hits the home tab again, when it's still highlighted, I'd like to reload the initial url where it started.选项卡 1 转到已加载其他页面的 web 视图,当用户再次点击主页选项卡时,当它仍然突出显示时,我想重新加载它开始的初始 url。

Thanks for any ideas!感谢您的任何想法!

The UITabBarControllerDelegate method [– tabBarController:didSelectViewController:] gets called each time the tab bar is touched.每次触摸标签栏时,都会调用 UITabBarControllerDelegate 方法[– tabBarController:didSelectViewController:] The documentation for this API states:此 API 的文档说明:

In iOS v3.0 and later, this (selected view controller) could be the same view controller that was already selected.在 iOS v3.0 及更高版本中,此(选定的视图控制器)可能与已选定的视图控制器相同。

So if you detect your specified tab being selected again, you can have this delegate method reload your initial URL.因此,如果您检测到您的指定选项卡再次被选中,您可以让此委托方法重新加载您的初始 URL。

@interface AHTabBarController () <UITabBarControllerDelegate>
@property (nonatomic, strong) UIViewController* previousViewController;
@end

/// ///

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController
{
    if ([viewController isEqual:self.previousViewController])
    {
          NSLog(@"reselect tabbar");
    }
    self.previousViewController = viewController;
}

Here is a full answer for common use cases.这是常见用例的完整答案。


  1. Create a protocol for handling the reselection创建处理重选的protocol

     protocol TabInteractionDelegate { func didReselectTab(at index: Int, with item: UITabBarItem) }
  2. Call the protocol from a Custom UITabBarController从自定义UITabBarController调用protocol

     class CustomTabBarController: UITabBarController, UITabBarControllerDelegate { var tabInteractionDelegate: TabInteractionDelegate? // ... override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) { // This will: // 1. Call when the tab is reselected (ie The tab does not switch) // 2. NOT get called when the tab is switching to a new tab and showing a new view controller if (tabBar.items?[selectedIndex] == item) { tabInteractionDelegate?.didReselectTab(at: selectedIndex, with: item) } } }
  3. Listen to changed in the UIViewController you need it in在你需要的UIViewController收听改变

    class CustomViewController: UIViewController, TabInteractionDelegate { override func viewDidLoad() { super.viewDidLoad() // Attach the delegate 👇 if let tabBarCont = tabBarController as? ChoiceTabBarController { tabBarCont.tabInteractionDelegate = self } } // Listen to the change 👇 func didReselectTab(at index: Int, with item: UITabBarItem) { print("\\(index) :: \\(item)") // Here you can grab your scrollview and scroll to top or something else } }

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM