选项卡栏在swift中显示错误的选定索引
在我的应用程序中,有四个选项卡的选项卡栏。通过单击显示不同视图控制器的任何选项卡。单击第三和第四选项卡,将显示相同的viewcontroler。在创建选项卡栏的过程中,我创建了同一viewcontroler的不同对象。并在第三和第四个选项卡中显示它们选项卡栏在swift中显示错误的选定索引,swift,tabbar,Swift,Tabbar,在我的应用程序中,有四个选项卡的选项卡栏。通过单击显示不同视图控制器的任何选项卡。单击第三和第四选项卡,将显示相同的viewcontroler。在创建选项卡栏的过程中,我创建了同一viewcontroler的不同对象。并在第三和第四个选项卡中显示它们 func configureViewController() { let storyBoard = UIStoryboard(name: "Main", bundle: nil) let Pat
func configureViewController() {
let storyBoard = UIStoryboard(name: "Main", bundle: nil)
let PatientListViewController = storyBoard.instantiateViewController(withIdentifier: "PatientListViewController") as? PatientListViewController
let DeliveryFormNavigationController = configureNavController(unselectedImage: imageLiteral(resourceName: "DeliveryFormTabUnselected") , selectedImage: imageLiteral(resourceName: "DeliveryFormTabSelected"), rootViewController: PatientListViewController!)
let PatientListViewController1 = storyBoard.instantiateViewController(withIdentifier: "PatientListViewController") as? PatientListViewController
let StockAndBillNavigationController = configureNavController(unselectedImage: imageLiteral(resourceName: "StockAndBillUnselected"), selectedImage: imageLiteral(resourceName: "StockAndBillSelected"), rootViewController: PatientListViewController1!)
let storyBoard1 = UIStoryboard(name: "Swipe", bundle: nil)
let allPdfFilesVC = storyBoard1.instantiateViewController(withIdentifier: "AllPdfFilesViewController") as? AllPdfFilesViewController
let allPdfNavigationController = configureNavController(unselectedImage: imageLiteral(resourceName: "AllPdfUnselected"), selectedImage: imageLiteral(resourceName: "AllPdfSelected"), rootViewController: allPdfFilesVC!)
let dashBoardViewController =
storyboard?.instantiateViewController(withIdentifier: "DashBoardViewController")
let DashBoardVC =
storyboard?.instantiateViewController(withIdentifier: "DashBoardVC")
let dashBoardNavigationController = configureNavController(unselectedImage: imageLiteral(resourceName: "DashboardTabUnselected"), selectedImage: imageLiteral(resourceName: "DashboardTabSelected"), rootViewController: DashBoardVC!)
// Add all the view instances to the tabbar
viewControllers = [dashBoardNavigationController, allPdfNavigationController, DeliveryFormNavigationController, StockAndBillNavigationController]
guard let items = tabBar.items else {return}
print(items)
for item in items {
if #available(iOS 13, *) {
item.imageInsets = UIEdgeInsets(top: 6, left: 0, bottom: 0, right: 0)
self.additionalSafeAreaInsets = UIEdgeInsets(top: 20, left: 0, bottom: 0, right: 0)
item.title = nil
}
}
}
但当我进入第三和第四个选项卡时,有时会显示选中的索引,如果是“2”,有时是“3”
class PatientListViewController: UIViewController {
override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)
if let app = UIApplication.shared.delegate as? AppDelegate, let window = app.window {
if let tabBarController = window.rootViewController as? UITabBarController {
if tabBarController.selectedIndex == 2{
pdfType = "Delivery Form"
}
else if tabBarController.selectedIndex == 3{
pdfType = "Stock and bill form"
}
}
}
}
}