从Tabbar更改ViewController

时间:2017-12-14 10:55:27

标签: swift uinavigationcontroller uitabbarcontroller

我有一个UITabBarController,其中包含3个标签和SettingsVC,您可以在其中选择MainVC的外观(根据用户偏好的内容,有2个不同的ViewControllers显示为MainVC)。

我制作了应用程序并且它有效,但它确实有问题,因为我只是将ViewControllers推到彼此之上,并且在某些情况下,您在切换tabs时会看到错误。

  

Main.storyboard

enter image description here

以下是我的代码的一部分(在尝试了很多内容之后)根据UserDefaults更改了VC的内容

  

CheckSettingsStatus.swift

//
// Check for Double Column Settings and navigate to Double Column VC
//
func checkMainVCViewStatusAndNavigateAcordingly(){
    if UserDefaults.standard.integer(forKey: KEY_MAIN_VC_VIEW) == 1{
        //let appDel = UIApplication.shared.delegate as! AppDelegate
        //appDel.goToDoubleMainVC()

        let mainVC = storyboard?.instantiateViewController(withIdentifier: "DoubleColumnMainVC") as! DoubleColumnMainVC
        self.navigationController?.viewControllers[0] = mainVC
        //navigationController?.pushViewController(mainVC, animated: false)

        var navigationArray = self.navigationController?.viewControllers
        print("Number of VCs = \(String(describing: navigationArray?.count))")
        if navigationArray?.count == nil{
            self.navigationController?.pushViewController(mainVC, animated: false)
        }else{
            self.navigationController?.popToRootViewController(animated: false)
        }
        //            self.navigationController?.viewControllers = navigationArray!
    }
}


//
// same as function above but goes to MainVC
//
func checkDoubleColumnMainVCViewStatusAndNavigateAcordingly(){
    if UserDefaults.standard.integer(forKey: KEY_MAIN_VC_VIEW) == 0{
        //            let appDel = UIApplication.shared.delegate as! AppDelegate
        //            appDel.goToMainVC()
        let mainVC = storyboard?.instantiateViewController(withIdentifier: "MainVC") as! MainVC
        self.navigationController?.viewControllers[0] = mainVC

        var navigationArray = self.navigationController?.viewControllers
        print("Number of VCs = \(String(describing: navigationArray?.count))")
        if navigationArray?.count == nil{
            navigationController?.pushViewController(mainVC, animated: false)
        }else{
            self.navigationController?.popToRootViewController(animated: false)
        }
        //            self.navigationController?.viewControllers = navigationArray!
    }
}

在这两个控制器的ViewWillAppear中,我调用这些函数进行相应的切换。

任何想法我做错了什么?我可以从UITabBarController推送控制器而不是NavigationController吗?类似的东西:

tabBarController.pushViewController(mainVC, animated: false)

而不是:

navigationController.pushViewController(mainVC, animated: false)

提前谢谢

更新

在我最好的情况下,当一切正常运行时,首次启动应用时(到第二个mainVCButtons中的NavBar无效。

修改

我刚刚意识到我在NavigationBar上面看到的SecondMainVC是来自NavigationBar的{​​{1}},这就是按钮不起作用的原因。怎么可能?

2 个答案:

答案 0 :(得分:2)

我在试图在故事板中开发标签栏控制器时遇到了同样的问题,因此我发现最好的方法是通过代码实现它。 所以你可以让你的代码看起来像这样:

1)创建一个UITabController的子类,让它符合UITabBarControllerDelegate

  class TabBarVC: UITabBarController , UITabBarControllerDelegate {

2)然后在你的类中编写这个方法来初始化项目中的选项卡:

  private func settingUpTabBarAndInitializingViewControllers(){

    //self delagting
    self.delegate = self 

    //home tab
    let homeStoryboard = UIStoryboard(name: Constants.StoryBoards.homeStoryboard, bundle: nil)
    let homeVC = homeStoryboard.instantiateViewController(withIdentifier: Constants.ControllersIDs.homeScreen) as! HomeVC

    // shop tab
    let shopStoybaord = UIStoryboard(name: Constants.StoryBoards.shopStoryboard, bundle: nil)
    let shopVC = shopStoybaord.instantiateViewController(withIdentifier: Constants.ControllersIDs.shopScreen) as! ShopVC

    //offers tab
    let offersStoryboard = UIStoryboard(name: Constants.StoryBoards.offersStoryboard, bundle: nil)
    let offersVC = offersStoryboard.instantiateViewController(withIdentifier: Constants.ControllersIDs.offersScreen) as! OffersVC

    //more tab
    let moreStoryboard = UIStoryboard(name: Constants.StoryBoards.MoreScreenStoryboard, bundle: nil)
    let moreVC = moreStoryboard.instantiateViewController(withIdentifier: Constants.ControllersIDs.moreVCScreen) as! MoreOptionsTVC


    //setting VCs
    self.viewControllers = [homeVC , shopVC , offersVC , moreVC]


    //setting buttons

    //buttons images
    let homeImage = UIImage(named: "TabHome")
    let shopImage = UIImage(named : "TabShop")
    let offerImage = UIImage(named: "TabOffers")
    let moreImage = UIImage(named: "TabMenu")


    // buttons shakhsiyan
    let homeButton = UITabBarItem(title: homeText, image: homeImage, selectedImage: homeTappedImage)
    let shopButton = UITabBarItem(title: shopText, image: shopImage, selectedImage: shopTappedImage)
    let offersButton = UITabBarItem(title: offersText, image: offerImage, selectedImage: offersTappedImage)     
    let moreButton = UITabBarItem(title: moreText, image: moreImage, selectedImage: moreTappedImage)


    homeVC.tabBarItem = homeButton
    shopVC.tabBarItem = shopButton
    offersVC.tabBarItem = offersButton
    moreVC.tabBarItem = moreButton

}

P.S。你可以根据需要添加标签,在滚动它们时你不会遇到任何错误,

确保标签栏控制器前只有一个导航控制器。

答案 1 :(得分:2)

检查这希望它会有所帮助 enter image description here

好的,你可以创建一个DEMO来看看它是如何工作的。之后你可以在你的项目中实现它。我知道它可能看起来不太好但是只读一遍并看到附加的图像我肯定会得到你的答案。

1.我使用UITabBarController创建了两个UIViewControllers。

2.我创建了类ItemFirstVC,ItemTwoVC并在故事板中分配给UIViewControllers

3.每个控制器都嵌入在UINavigationController

4.创建一个UITabBarController的自定义类

5.在故事板中将其分配给UITabBarController

6.现在每个UIViewController都有一个名为Firs Button的按钮,第二个按钮

7.从每个按钮我已将Action Segue连接到另一个控制器,如图所示。

8.现在我已经创建了第三个与UITabBarController分开的UIViewController。

9.现在我们将在UITabBarController中加载第三个UIViewController

10.再创建一个ItemThirdVC类并将其分配给第三个UIViewController。

11.它还有一个名为第三个按钮动作的按钮,它被另一个UIViewController控制

12.现在切换到UITabBarContoller的自定义类

if len(contours) > 1:
    cnt1 = contours[0]
    cnt2 = contours[1]
    area = cv2.contourArea(cnt1)
    M1 = cv2.moments(cnt1)
    M2 = cv2.moments(cnt2)
    if area > 30:
        cv2.drawContours(frame, contours, -1, (0, 255, 255), 3)
        X1_px = int(M1['m10'] / M1['m00'])
        Y1_px = int(M1['m01'] / M1['m00'])
        X2_px = int(M2['m10'] / M1['m00'])
        Y2_px = int(M2['m01'] / M1['m00'])
        cv2.circle(frame, (X1_px, Y1_px), 3, (0, 0, 255), -1)
        cv2.circle(frame, (X2_px, Y2_px), 3, (0, 0, 255), -1)

        if (X2_px-X1_px) != 0:
            angle = math.atan((Y2_px-Y1_px)/(X2_px-X1_px))
            angle = int(angle * (180 / math.pi))
            print(angle)