在选择标签栏项目时呈现视图控制器

present view controller on selecting tabbar item

如何在点击标签栏项目时显示视图控制器。 我只想显示为弹出窗口。 使用故事板

我找到了解决方案,但这不起作用

  AddImage *yourViewController= (AddImage*)  [self.tabBarController.viewControllers objectAtIndex:3];

    CGFloat tabBarHeight = self.tabBarController.tabBar.bounds.size.height;
    CGRect rect = CGRectMake(0, 0, tabBarHeight, tabBarHeight);
    [AddImage presentPopoverFromRect:rect
                                                                             inView:self.tabBarController.tabBar
                                                           permittedArrowDirections:UIPopoverArrowDirectionDown animated:YES];

为了这个目的
1) 设置tabbar的委托

2) 实现方法:-(void)tabBar:(UITabBar *)tabBar didSelectItem:(UITabBarItem *)item

3) 现在显示特定项目的弹出窗口

Swift 5

创建一个继承自 UITabBarController 的 class 并设置协议 UITabBarControllerDelegate

class CustomTabController: UITabBarController, UITabBarControllerDelegate {

   override func viewDidLoad() {
       super.viewDidLoad()
       self.delegate = self 
   }
}

实施方法tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool

从那里开始,一个简单的 if-else 语句来检查当前控制器 class 是什么就足够了。

func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
    if viewController is YourViewController {
        self.present(YourViewController(), animated: true)
        return false
    } else {
        return true
    }
}