Ios 将数据从CollectionView传递给TabBarController,而不是Swift中的孩子
我不熟悉斯威夫特。无法找到以下问题的解决方案。 下面是一个带有CollectionView的ViewController,当您单击CollectionView中的单元格时,单元格中的数据(即使该单元格不在标签和图像视图中,但在书本数组行中)必须发送到TabBarCollection,而不是TabBarCollection,我需要将该数据发送到所有子单元格,如下图所示。 稍后在TabBar的childs中,我将根据所选单元格中的数据设置视图控制器中标签的值。 斯威夫特Ios 将数据从CollectionView传递给TabBarController,而不是Swift中的孩子,ios,swift,collectionview,Ios,Swift,Collectionview,我不熟悉斯威夫特。无法找到以下问题的解决方案。 下面是一个带有CollectionView的ViewController,当您单击CollectionView中的单元格时,单元格中的数据(即使该单元格不在标签和图像视图中,但在书本数组行中)必须发送到TabBarCollection,而不是TabBarCollection,我需要将该数据发送到所有子单元格,如下图所示。 稍后在TabBar的childs中,我将根据所选单元格中的数据设置视图控制器中标签的值。 斯威夫特 import UIKit
import UIKit
struct Book {
let title: String
let image: UIImage?
//Here soon will be more "let", and this data will also have to be send to TabBar but it don't will be show I CollectionViewCell
}
extension Book {
static let books: [Book] = [
Book(title: "Antygona", image: UIImage(named: "imgantygona")!),
//etc
]
}
CollectionViewCell.swift
import UIKit
class CollectionViewCell: UICollectionViewCell {
@IBOutlet weak var bookImageView: UIImageView!
@IBOutlet weak var bookTitle: UILabel!
func setup(with book: Book) {
bookTitle.text = book.title
bookImageView.image = book.image
}
}
视图控制器
import UIKit
class ViewController: UIViewController, UICollectionViewDelegate, UICollectionViewDataSource {
@IBOutlet weak var collectionView: UICollectionView!
let books = Book.books
override func viewDidLoad() {
super.viewDidLoad()
let fontAttributes = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16.0)]
UITabBarItem.appearance().setTitleTextAttributes(fontAttributes, for: .normal)
collectionView.dataSource = self
collectionView.delegate = self
}
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return books.count
}
func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "bookCell", for: indexPath) as! CollectionViewCell
let book = books[indexPath.item]
cell.setup(with: book)
return cell
}
}
我看到了很多解决方案,但我不能完全适应我的问题(
谢谢你的帮助
bookindeviewcontroller.swift
import UIKit
class BookInsideViewController: UIViewController {
@IBOutlet weak var testImageView: UIImageView!
@IBOutlet weak var testLabel: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
}
}
您可以使用collection view DidSelectItemAt函数
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let tabBarController = storyboard.instantiateViewController(withIdentifier: "YourtabbarIdentifier") as! UITabBarController
// You should access the `imageController` through your `tabBarController`,
// not instantiate it from storyboard.
if let viewControllers = tabBarController.viewControllers,
let imageController = viewControllers.first as? ImageController {
BookInsideViewController.recivedData1 = Books[indexPath.row]
}
navigationController?.pushViewController(tabBarController, animated: true)
}
谢谢。我将TabBar中的情节提要ID设置为“bookTabBar”,然后将其替换为“YourTabbariIdentifier”,但现在我遇到了“使用未声明的类型“ImageController”和未解析的标识符“Books”的问题,我不知道如何按您所说的做”您应该通过
tabBarController
访问imageController
,而不是从故事板中实例化它。“您能告诉我怎么做吗?谢谢您的帮助:)