import UIKit
class TabBarController: UITabBarController, UITabBarControllerDelegate {
override func viewDidLoad() {
super.viewDidLoad()
view.backgroundColor = .white
self.delegate = self
let vc1 = UINavigationController(rootViewController: VideoListVC())
let vc2 = UINavigationController(rootViewController: AddViewController())
let vc3 = UINavigationController(rootViewController: FabricViewController())
vc1.tabBarItem.image = UIImage(systemName: "house")
vc2.tabBarItem.image = UIImage(systemName: "qrcode")
vc3.tabBarItem.image = UIImage(systemName: "house")
//tabBar.tintColor = .label
setViewControllers([vc1, vc2, vc3], animated: true)
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UITabBarController) {
if viewController is AddViewController {
print("Alert is opened")
}
}
I can’t open alertcontroller because my TabBarController is rootcontroller. How can i fix it ?
3
Answers
self.show(alertController, sender: self)
This should work as
UITabbarController
is a subclass ofUIViewController
the didSelect method is wrong , replace with :
Just cross check you delegate function with the below code
Since you are adding
UINavigationController
to theUITabBarController
, so first you need to check for that inside the delegate function and then check for the rooviewcontroller of theUINavigationController
.