简体   繁体   English

将通过故事板制作的 UIViewController 添加到 TabBarController.setViewControllers

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

I am failing to understand the fundamentals of what is needed to add my HomeViewController (UIViewController) as one of the tabs in my homeTabBarController (UITabBarController) using the setViewControllers method.我无法理解使用 setViewControllers 方法将我的 HomeViewController (UIViewController) 添加为我的 homeTabBarController (UITabBarController) 中的选项卡之一所需的基础知识。

I have tried initializing this and simply adding it as a param in the method.我试过初始化它并简单地将它作为参数添加到方法中。 There seems to be a difference between a view controller created via storyboard and one created programmatically because when I tried adding a viewcontroller:UIViewController programmatically to the setViewControllers method, it worked fine.通过故事板创建的视图控制器与以编程方式创建的视图控制器之间似乎存在差异,因为当我尝试以编程方式将 viewcontroller:UIViewController 添加到 setViewControllers 方法时,它工作正常。

My code below compiles however I get a runtime exception Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ff7b8491598) at the line when homeTabBarController.setViewControllers is called我的下面的代码编译但是我得到一个运行时异常 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.

相关问题 向故事板中的tabbarController添加更多视图 - add more views to tabbarController in storyboard 在 UIViewController 中添加 Storyboard 作为子视图 - Add Storyboard as subview in UIViewController 如何通过有条件加载UIViewController的情节提要添加UITabBarItem - How to add UITabBarItem via storyboard that conditionally loads a UIViewController TabBarController-> NavigationController->使用Storyboard的多个UIViewController(rootview控制器) - TabBarController -> NavigationController -> Multiple UIViewController (rootview controllers) using Storyboard 从另一个Storyboard添加UIViewController - Add UIViewController from another Storyboard 在TabBarController中保留UIViewController - Retain UIViewController in TabBarController 我可以将私有变量传递到将通过情节提要实例化的UIViewController吗? - Can I pass in private variables to a UIViewController that will be instantiate via a Storyboard? iOS6:如何通过标识符在Storyboard中连接UIViewController? - iOS6: How to connect UIViewController in Storyboard via Identifier? TabBarController:访问UIViewController内部的属性 - TabBarController: accessing a property of inside UIViewController 在tabBarController的UIViewController上调用实例方法 - calling an instance method on a UIViewController in a tabBarController
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM