gpt4 book ai didi

swift - 我试图将变量从 UITabController 传递到 UIViewController 但没有运气?

转载 作者:行者123 更新时间:2023-11-30 10:44:54 25 4
gpt4 key购买 nike

我有一个 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时要显示的 View ,我想将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)")
}
}

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

最佳答案

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

您要修改的属性是 NewFileButtonPressedTabController 类中的变量 firstViewControllerInTabBar 中的属性,因为您的 View Controller 的副本是正在显示的。所以你的 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

}

}

关于swift - 我试图将变量从 UITabController 传递到 UIViewController 但没有运气?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943149/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com