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 ?
CodePudding user response:
the didSelect method is wrong , replace with :
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if viewController is AddViewController {
print("put code for showing alert")
}
}
CodePudding user response:
Just cross check you delegate function with the below code
func tabBarController(_ tabBarController: UITabBarController, didSelect viewController: UIViewController) {
if let vc = viewController as? UINavigationController, vc.topViewController! is AddViewController {
let alert = UIAlertController(title: "Alert", message: "Message", preferredStyle: .alert)
alert.addAction(UIAlertAction(title: "Ok", style: .default))
self.present(alert, animated: true)
}
}
Since you are adding UINavigationController
to the UITabBarController
, so first you need to check for that inside the delegate function and then check for the rooviewcontroller of the UINavigationController
.
CodePudding user response:
self.show(alertController, sender: self)
This should work as UITabbarController
is a subclass of UIViewController