如何从集合视图控制器打开新视图

时间:2016-11-11 16:51:38

标签: ios swift

我是iOS的新手,并且很难从集合视图中打开新的视图控制器。

任何人都可以帮我解决如何打开新的视图控制器并传递日期,以便我可以在另一个视图中更改标签文本。

1 个答案:

答案 0 :(得分:1)

以下是如何操作:

SecondViewController代码:

import UIKit

class SecondViewController: UIViewController
{
    var date : NSDate!
    @IBOutlet weak var dateLabel: UILabel!

    override func viewDidLoad()
    {
        super.viewDidLoad()
        self.dateLabel.text = date.description
    }
}

FirstViewController代码:

实施didSelectItemAt:indexPath:

UICollectionViewDelegate方法
func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath)
    {
        let secondVC = self.storyboard?.instantiateViewController(withIdentifier: "SecondViewController") as! SecondViewController
        secondVC.date = NSDate()
   self.navigationController.pushViewController(secondVC, animated: true)
   }
相关问题