gpt4 book ai didi

ios - 在没有 Storyboard/xib 的 View Controller 之间导航

转载 作者:行者123 更新时间:2023-11-30 12:02:58 25 4
gpt4 key购买 nike

我正在开发一个没有 Storyboard/xib 的应用程序,这被证明是一个挑战。我有一个(广告的)UICollectionView,用户可以点击单元格导航到新的 View Controller 以显示该广告的详细信息。下面是我遇到的错误的 gif。

enter image description here

如您所见,在我点击单元格后,UICollectionView 类型会保留下来。我的 AdsViewController 子类中有一个 adDetailsViewController 属性,当我点击单元格时,此代码将执行:

adDetailsViewController.adTitle = adTitles[indexPath.row]
navigationController?.pushViewController(adDetailsViewController, animated: true)

以下是 adDetailsViewController 的生命周期方法(目前只是 viewDidLoad):

class AdDetailsViewController: UIViewController {

var adTitle: String?

override func viewDidLoad() {
super.viewDidLoad()

let label = UILabel(frame: view.bounds)
label.textAlignment = .center
label.center = view.center
label.text = adTitle

view.addSubview(label)
}
}

为了完整起见,以下是 AdsViewController 的其余部分:

class AdsViewController: UIViewController {

lazy var collectionView: AdsCollectionView = {
let collectionView = AdsCollectionView(frame: view.frame, collectionViewLayout: TiledFlowLayout())
collectionView.delegate = self
collectionView.dataSource = self
collectionView.register(AdCollectionViewCell.self, forCellWithReuseIdentifier: "AdCell")
view.addSubview(collectionView)
return collectionView
}()

let adDetailsViewController = AdDetailsViewController()

let adTitles = ["1", "2", "3", "4", "5", "6"]

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.

collectionView.translatesAutoresizingMaskIntoConstraints = false
collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: 8).isActive = true
collectionView.leadingAnchor.constraint(equalTo: view.leadingAnchor, constant: 0).isActive = true
collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor, constant: 0).isActive = true
collectionView.trailingAnchor.constraint(equalTo: view.trailingAnchor, constant: 0).isActive = true

view.backgroundColor = .white
}
}

extension AdsViewController: UICollectionViewDataSource {
func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
return adTitles.count
}

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "AdCell", for: indexPath) as! AdCollectionViewCell

cell.textLabel.text = adTitles[indexPath.row]

return cell
}
}

extension AdsViewController: UICollectionViewDelegate {
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
adDetailsViewController.adTitle = adTitles[indexPath.row]
navigationController?.pushViewController(adDetailsViewController, animated: true)
}
}

extension AdsViewController: UICollectionViewDelegateFlowLayout {
func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
return CGSize(width: (self.view.frame.size.width / 2) - 10, height: self.view.frame.size.height / 4)
}

func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
return UIEdgeInsets(top: 0, left: 5, bottom: 0, right: 5)
}
}

最佳答案

好的,我修好了。看来,如果我将 AdDetailsViewControllerbackgroundColor 设置为 UIColor.white,那么它会按预期进行转换。

关于ios - 在没有 Storyboard/xib 的 View Controller 之间导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46992842/

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