我试图将变量从UITabController传递给UIViewController,但没有运气吗?

时间:2019-05-01 21:51:58

标签: swift xcode parameter-passing

我有一个UITabController类,在其中创建了一个自定义UINavigationBar的实例,我希望将其高度传递给另一个UIViewController。以下是我在UITabController类中拥有的代码:

import UIKit

class NewFileButtonPressedTabController: UITabBarController, UINavigationBarDelegate
{
    let firstViewControllerInTabBar = FirstItemInTabBarOpenRolledSections()
    let secondViewControllerInTabBar = SecondItemInTabBarHollowStructuralSections()

    let navigationBar = CustomNavigationBar(navigationBarTitle: "'Blue Book' Catalogue Sections", leftBarButtonTarget: self, leftBarButtonSelector: #selector(buttonPressed(sender:)))

    override func viewDidLoad() {

        super.viewDidLoad()

        navigationBar.delegate = self
        view.addSubview(navigationBar)

        firstViewControllerInTabBar.tabBarItem.image = UIImage(named: "notSelectedStateOpenSections")?.withRenderingMode(.alwaysOriginal)
        firstViewControllerInTabBar.tabBarItem.selectedImage = UIImage(named: "selectedOpenSections")?.withRenderingMode(.alwaysOriginal)
        firstViewControllerInTabBar.tabBarItem.tag = 0
        secondViewControllerInTabBar.tabBarItem.image = UIImage(named: "notSelectedHollowSections")?.withRenderingMode(.alwaysOriginal)
        secondViewControllerInTabBar.tabBarItem.selectedImage = UIImage(named: "selectedHollowSections")?.withRenderingMode(.alwaysOriginal)
        secondViewControllerInTabBar.tabBarItem.tag = 1
        let tabBarList = [firstViewControllerInTabBar, secondViewControllerInTabBar]

        viewControllers = tabBarList
    }

    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        navigationBar.leftAnchor.constraint(equalTo: view.leftAnchor).isActive = true
        navigationBar.rightAnchor.constraint(equalTo: view.rightAnchor).isActive = true

        if #available(iOS 11, *) {
            navigationBar.topAnchor.constraint(equalTo: view.safeAreaLayoutGuide.topAnchor).isActive = true
        } else {
            navigationBar.topAnchor.constraint(equalTo: view.topAnchor).isActive = true
        }

        override func viewWillAppear(_ animated: Bool) {
            setupConstraints()
        }


        func setupConstraints() {
            firstViewControllerInTabBar.view.translatesAutoresizingMaskIntoConstraints = false
            firstViewControllerInTabBar.view.topAnchor.constraint(equalTo: navigationBar.bottomAnchor).isActive = true
            firstViewControllerInTabBar.view.bottomAnchor.constraint(equalTo: tabBar.topAnchor).isActive = true
            firstViewControllerInTabBar.view.leftAnchor.constraint(equalTo: self.view.leftAnchor).isActive = true
            firstViewControllerInTabBar.view.rightAnchor.constraint(equalTo: self.view.rightAnchor).isActive = true
        }
    }

    func position(for bar: UIBarPositioning) -> UIBarPosition {
        return UIBarPosition.topAttached
    }

    override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {
        if tabBarItem.tag == 0 {
            let sb = storyboard?.instantiateViewController(withIdentifier: "FirstItemInTabBarOpenRolledSections") as! FirstItemInTabBarOpenRolledSections
            print("tag 0 has been pressed")
            print(navigationBar.frame.size.height)
            sb.tabBarControllerNavigationBarHeight = navigationBar.frame.size.height
        }
    }
}

下面是UIViewController中的代码,基本上是按下第一个TabBarItem时要显示的视图,我想将navigationBar的高度传递给:

import UIKit

class FirstItemInTabBarOpenRolledSections: UIViewController, UICollectionViewDataSource, UICollectionViewDelegate {

    var tabBarControllerNavigationBarHeight: CGFloat?

    override func viewDidLoad() {
        super.viewDidLoad()
        let customCollectionView = CustomCollectionView(layoutTopEdgeInset: 20, layoutBottomEdgeInset: 20, layoutLeftEdgeInset: 20, layoutRightEdgeInset: 20, totalWidthOfCollectionView: self.view.frame.width, totalHeightOfCollectionView: self.view.frame.height, minimumLayoutCellsHorizontalSpacing: 20, minimumLayoutCellsVerticalSpacing: 20, numberOfCellsPerRow: 2, numberOfCellsPerColumn: 4, viewThatCustomCollectionViewWillBeAddedTo: self.view, hostViewDelegate: self, hostViewDataSource: self)
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 8
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let myCell = collectionView.dequeueReusableCell(withReuseIdentifier: "MyCell", for: indexPath)
        myCell.backgroundColor = .blue
        return myCell
    }

    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("User tapped on itme \(indexPath.row)")
    }
}

但是,每次我在第二个视图内打印导航栏高度的值时,我总是得到nil,这意味着该值未成功传递。有人可以帮我弄清楚我做错了什么吗?

1 个答案:

答案 0 :(得分:0)

似乎正在发生的事情是,您正在使用FirstItemInTabBarOpenRolledSections函数storyboard?.instantiateViewController实例化override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)的新副本。然后,您要为View Controller的这个全新副本设置所需的属性-与正在显示的View Controller无关。

要修改的属性是firstViewControllerInTabBar类中变量NewFileButtonPressedTabController的属性,因为正在显示的是视图控制器的副本。因此,您的tabBar的didSelect方法应如下所示:

override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem) {

        if tabBarItem.tag == 0 {

            print("tag 0 has been pressed")

            print(navigationBar.frame.size.height)

            firstViewControllerInTabBar.tabBarControllerNavigationBarHeight = navigationBar.frame.size.height

        }

    }
相关问题