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

问题描述 投票:0回答:1

我有一个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,这意味着该值未成功通过。有人可以帮我弄清楚我做错了什么吗?

swift xcode parameter-passing
1个回答
0
投票

似乎正在发生的是你使用FirstItemInTabBarOpenRolledSections storyboard?.instantiateViewController函数实例化你的override func tabBar(_ tabBar: UITabBar, didSelect item: UITabBarItem)的新副本。然后,您将为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

        }

    }
© www.soinside.com 2019 - 2024. All rights reserved.