簡體   English   中英

將通過故事板制作的 UIViewController 添加到 TabBarController.setViewControllers

[英]Add UIViewController made via storyboard to TabBarController.setViewControllers

我無法理解使用 setViewControllers 方法將我的 HomeViewController (UIViewController) 添加為我的 homeTabBarController (UITabBarController) 中的選項卡之一所需的基礎知識。

我試過初始化它並簡單地將它作為參數添加到方法中。 通過故事板創建的視圖控制器與以編程方式創建的視圖控制器之間似乎存在差異,因為當我嘗試以編程方式將 viewcontroller:UIViewController 添加到 setViewControllers 方法時,它工作正常。

我的下面的代碼編譯但是我得到一個運行時異常 Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ff7b8491598) 在 homeTabBarController.setViewControllers 被調用時

`

    func loadTabBar() {
        let storyboard = UIStoryboard(name: "Main", bundle: nil)
        let homeViewController = storyboard.instantiateViewController(identifier: Constants.Storyboard.homeViewController) as? HomeViewController

        homeViewController!.title = "Home"

        homeTabBarController.setViewControllers([homeViewController!], animated: false)
        homeTabBarController.modalPresentationStyle = .fullScreen
        present(homeTabBarController, animated: true)
    }

`

  //MARK: - Create the instances of ViewControllers
    let grayViewController = HomeViewController()
    let blueViewController = FirstViewController()
    let brownViewController = SecondViewController()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        //set title of the viewcontrollers
        grayViewController.title = "home"
        blueViewController.title = "first"
        brownViewController.title = "second"
        
        //Assigne the viewcontrollers to the TabBarViewController
        self.setViewControllers([ grayViewController, blueViewController, brownViewController], animated: false)
        
        //set system images to each tabBars
        guard let items = self.tabBar.items  else {
            return
        }
        
        let images = ["house.fill", "star.fill", "bell.fill"]
        
        for i in 0...2 {
            items[i].image = UIImage(systemName: images[i])
        }
        
        self.tabBar.tintColor = .black
        
    }

// You can download the project from the below github link


https://github.com/ahmetbostanciklioglu/AddingTabBarControllerProgrammatically.git

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM