选择UICollectionViewCell并执行segue

时间:2018-04-30 10:51:43

标签: swift xcode

希望将用户选定的集合单元格标题存储为局部变量,然后可以使用segue将其传递到下一个视图。我遇到了didSelectItemAt函数的问题,即使我在那里放了一个print语句,当选择一个单元格时也没有任何反应。

  

GoalsViewController

import Foundation
import UIKit

class GoalsViewController: UIViewController {

    @IBOutlet weak var collectionView: UICollectionView!

    var selectedGoal: String = ""

    var goalArray = ["Goal 1", "Goal 2"]
    var imageArray = ["1", "2"]

    override func viewDidLoad() {
        super.viewDidLoad()

        collectionView.dataSource = self
        collectionView.delegate = self
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if segue.identifier == "goToGoalDetail" {
            let secondVC = segue.destination as! GoalDetailViewController
            secondVC.goalSelectedOnHome = selectedGoal
        }
    }



}

extension GoalsViewController: UICollectionViewDataSource, UICollectionViewDelegate {
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }

    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return goalArray.count
    }

    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        selectedGoal = goalArray[indexPath.row]
    }

    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "GoalCell", for: indexPath) as! GoalsCollectionViewCell
        cell.goalTitleLabel.text = goalArray[indexPath.row]
        cell.backgroundColor = UIColor.darkGray
        cell.featuredImageView.image = UIImage(named: "\(imageArray[indexPath.row])")

        return cell
    }


}
  

GoalsCollectionViewCell

import UIKit

class GoalsCollectionViewCell: UICollectionViewCell {

    @IBOutlet weak var featuredImageView: UIImageView!
    @IBOutlet weak var goalTitleLabel: UILabel!
    @IBOutlet weak var backgroundColorView: UIView!
}
  

GoalsDetailViewController

import UIKit

class GoalDetailViewController: UIViewController {
    @IBOutlet weak var closeButtonImage: UIImageView!

    @IBOutlet weak var goalDetailTitle: UILabel!
    @IBOutlet weak var closeButton: UIButton!

    var goalSelectedOnHome = ""

    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.

        goalDetailTitle.text = goalSelectedOnHome

        closeButton.setImage(UIImage(named: "closeIcon"), for: .normal)
        closeButton.addTarget(self, action: #selector(closeButtonTapped), for: .touchUpInside)

    }

    @objc func closeButtonTapped(sender:UIButton!) {
        self.dismiss(animated: true, completion: nil)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }



}

0 个答案:

没有答案