在没有storyboards / xib的视图控制器之间导航

时间:2017-10-28 18:02:36

标签: ios swift uicollectionview

我正在开发一个没有storyboards / xibs的应用程序,这被证明是一个挑战。我有UICollectionView(广告),用户可以点按某个单元格导航到新的视图控制器,以显示该广告的详细信息。下面是我所遇到的错误的GIF。

enter image description here

正如您所看到的,点击单元格后,UICollectionView类型的东西就会被粘住。我的adDetailsViewController子类中有一个AdsViewController属性,当我点击该代码执行的单元格时:

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

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

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)
    }
}

2 个答案:

答案 0 :(得分:0)

您必须分享更多代码。 f.e.有什么特殊设置吗?像透明的背景?你在viewDidLoad做什么?你是否覆盖了viewWillAppear,viewDidAppear等,并没有调用子类函数(如super.viewWillAppear(animated))?这是一个非常常见的错误......

答案 1 :(得分:0)

好的,我修好了。似乎如果我将backgroundColor的{​​{1}}设置为AdDetailsViewController,那么它会按预期进行转换。