Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 以编程方式传递数据时出现问题_Ios_Swift_Swift3_Programmatically Created - Fatal编程技术网

Ios 以编程方式传递数据时出现问题

Ios 以编程方式传递数据时出现问题,ios,swift,swift3,programmatically-created,Ios,Swift,Swift3,Programmatically Created,我无法以编程方式将数据从一个UICollectionViewController传递到另一个UICollectionViewController 目前我的设置如下: 正在传递数据的UICollectionViewController(RestaurantController) 1a。UICollectionViewCell(RestaurantCell) 此UICollectionViewCell中有一个嵌套的UICollectionViewController和另一个自定义UICollect

我无法以编程方式将数据从一个UICollectionViewController传递到另一个UICollectionViewController

目前我的设置如下:

  • 正在传递数据的UICollectionViewController(
    RestaurantController

    1a。UICollectionViewCell(
    RestaurantCell

    • 此UICollectionViewCell中有一个嵌套的UICollectionViewController和另一个自定义UICollectionViewCell(
      RestaurantCollectionViewCell
  • 接收数据的UICollectionViewController(
    MenuController

    2a。UICollectionViewCell(
    MenuCell

  • 在我的
    RestaurantCell
    中,我从JSON加载数据,并将其附加到一个称为restaurants的新数组中:
    var restaurants=[RestaurantModel]()
    。但是,当我尝试使用
    var RestaurantModel?
    在我的
    MenuController
    中加载餐厅名称或任何餐厅对象时,我得到的值为零。我有一种感觉,要么我的设置不正确,要么我在某处犯了一个愚蠢的错误。也许两者都有。我已经为每个类粘贴了下面的代码

    其中值在MenuController内返回nil:

    打印(“餐厅名称:”,餐厅?.Name)

    打印(“餐厅Id:”,餐厅?.Id)

    是自定义委派导致问题吗?

    非常感谢您的帮助和建议

    我的
    餐厅控制器的内部

     import UIKit
     import FBSDKLoginKit
    
     class RestaurantController: UICollectionViewController, UICollectionViewDelegateFlowLayout, SWRevealViewControllerDelegate, UISearchBarDelegate, RestaurantDelegate {
    
    var restaurantCell: RestaurantCell?
    
    private let restaurantCellId = "restaurantCellId"
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView?.backgroundColor = UIColor.qpizzaWhite()
        collectionView?.register(RestaurantCell.self, forCellWithReuseIdentifier: restaurantCellId)
    
    
        if self.revealViewController() != nil {
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "icon_menu_24dp").withRenderingMode(.alwaysOriginal), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    
    }
    
    // FIXME: issue with this...navigationcontroller is presenting, not pushing ontop of stack view
    func didTapRestaurantCell(cell: RestaurantCell) {
        print("Did Tap Restaurant Cell - Restaurant Controller")
    
        let layout = UICollectionViewFlowLayout()
        let controller = MenuController(collectionViewLayout: layout)
        navigationController?.pushViewController(controller, animated: true)
    
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: restaurantCellId, for: indexPath) as! RestaurantCell
        cell.delegate = self
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1
    }
    }
    
    import UIKit
    
    class MenuController: UICollectionViewController, UICollectionViewDelegateFlowLayout, SWRevealViewControllerDelegate {
    
    private let menuCellId = "menuCellId"
    
    var restaurant: RestaurantModel?
    var menuItems = [MenuItemsModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView?.backgroundColor = UIColor.qpizzaWhite()
        collectionView?.register(MenuCell.self, forCellWithReuseIdentifier: menuCellId)
        collectionView?.alwaysBounceVertical = true
    
        if self.revealViewController() != nil {
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "menu2-black-32").withRenderingMode(.alwaysOriginal), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    
        print("Restaurant Name:", restaurant?.name) // returns nil
        if let restaurantName = restaurant?.name {
            self.navigationItem.title = restaurantName
        }
    
        loadMenuItems()
    
    }
    
    func loadMenuItems() {
        print("Restaurant Id:", restaurant?.id) // returns nil
        if let restaurantId = restaurant?.id {
            print("RestaurantId:", restaurantId)
        }
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: menuCellId, for: indexPath) as! MenuCell
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let layout = UICollectionViewFlowLayout()
        let controller = MenuDetailsController(collectionViewLayout: layout)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 120)
    }
    
    }
    
    func didTapRestaurantCell(cell: RestaurantCell, withMenuController controller: MenuController) {
        print("Did Tap Restaurant Cell - Restaurant Controller")
        navigationController?.pushViewController(controller, animated: true)
    }
    
    在我的
    餐厅小区内

    protocol RestaurantDelegate {
        func didTapRestaurantCell(cell: RestaurantCell)
    }
    
    
    class RestaurantCell: BaseCell, UISearchBarDelegate, UICollectionViewDelegate, UICollectionViewDataSource, UICollectionViewDelegateFlowLayout {
    
    var delegate: RestaurantDelegate?
    var restaurants = [RestaurantModel]()
    var filteredRestaurants = [RestaurantModel]()
    
    private let restaurantCollectionViewCell = "restaurantCollectionViewCell"
    private let activityIndicator = UIActivityIndicatorView()
    
    lazy var searchBar: UISearchBar = {
        let sb = UISearchBar()
        sb.placeholder = "Search Restaurant"
        sb.barTintColor = .white
        UITextField.appearance(whenContainedInInstancesOf: [UISearchBar.self]).backgroundColor = UIColor.qpizzaWhite()
        sb.delegate = self
        return sb
    }()
    
    lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout()
        let cv = UICollectionView(frame: .zero, collectionViewLayout: layout)
        cv.backgroundColor = .white
        return cv
    }()
    
    override func setupViews() {
        super.setupViews()
        collectionView.register(RestaurantCollectionViewCell.self, forCellWithReuseIdentifier: restaurantCollectionViewCell)
        collectionView.delegate = self
        collectionView.dataSource = self
    
        backgroundColor = UIColor.qpizzaRed()
    
        addSubview(searchBar)
        addSubview(collectionView)
    
        _ = searchBar.anchor(topAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 4, leftConstant: 4, bottomConstant: 0, rightConstant: 4, widthConstant: 0, heightConstant: 50)
    
        _ = collectionView.anchor(searchBar.bottomAnchor, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 0, leftConstant: 0, bottomConstant: 0, rightConstant: 0, widthConstant: 0, heightConstant: 0)
    
         loadRestaurants()
    
    }
    
    func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
        print(searchText)
    
        filteredRestaurants = self.restaurants.filter({ (restaruant: RestaurantModel) -> Bool in
    
            return restaruant.name?.lowercased().range(of: searchText.lowercased()) != nil
        })
    
        self.collectionView.reloadData()
    }
    
    // MARK - Helper Methods
    func loadRestaurants() {
    
        showActivityIndicator()
    
        APIManager.shared.getRestaurants { (json) in
            if json != .null {
                //                print("Restaurant JSON:", json)
                self.restaurants = []
    
                if let restaurantList = json["restaurants"].array {
                    for item in restaurantList {
                        let restaurant = RestaurantModel(json: item)
                        self.restaurants.append(restaurant)
                    }
                    self.collectionView.reloadData()
                    self.hideActivityIndicator()
                }
            } else {
                print("Error loading JSON into Restaurant ViewController")
            }
        }
    }
    
    func loadImage(imageView: UIImageView, urlString: String) {
    
        let imageUrl: URL = URL(string: urlString)!
        URLSession.shared.dataTask(with: imageUrl) { (data, response, error) in
            if let error = error {
                print("Error loading image for Restaurant Controller:", error.localizedDescription)
            }
            guard let data = data, error == nil else { return }
    
            DispatchQueue.main.async(execute: {
                imageView.image = UIImage(data: data)
            })
            }.resume()
    }
    
    func showActivityIndicator() {
        activityIndicator.frame = CGRect(x: 0.0, y: 0.0, width: 40.0, height: 40.0)
        activityIndicator.center = center
        activityIndicator.hidesWhenStopped = true
        activityIndicator.activityIndicatorViewStyle = UIActivityIndicatorViewStyle.whiteLarge
        activityIndicator.color = UIColor.qpizzaGold()
    
        addSubview(activityIndicator)
        activityIndicator.startAnimating()
    }
    
    func hideActivityIndicator() {
        activityIndicator.stopAnimating()
    }
    
    //MARK: CollectionView Delegate & DataSource Methods
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: restaurantCollectionViewCell, for: indexPath) as! RestaurantCollectionViewCell
        let restaurant: RestaurantModel
    
        if searchBar.text != "" {
            restaurant = filteredRestaurants[indexPath.item]
        } else {
            restaurant = restaurants[indexPath.item]
        }
    
        cell.restaurantNameLabel.text = restaurant.name
        cell.restaurantAddressLabel.text = restaurant.address
    
        if let logoName = restaurant.logo {
            let url = "\(logoName)"
            loadImage(imageView: cell.restaurantLogoImageView, urlString: url)
        }
    
        return cell
    }
    
    
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        if searchBar.text != "" {
            return self.filteredRestaurants.count
        }
    
        return self.restaurants.count
    }
    
    //FIXME: Restaurant Name Navigation Title is still not be passed from RestaurantCell to MenuController
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Did Select Item - Restaurant Cell")
    
        let layout = UICollectionViewFlowLayout()
        let controller = MenuController(collectionViewLayout: layout)
        controller.restaurant = self.restaurants[indexPath.item]
    
        print("Controller", controller.restaurant) // Optional(QpizzaDelivery.RestaurantModel)
        print("Restaurant:", self.restaurants) // [QpizzaDelivery.RestaurantModel, QpizzaDelivery.RestaurantModel, QpizzaDelivery.RestaurantModel]
        print("IndexPath:", self.restaurants[indexPath.item]) // QpizzaDelivery.RestaurantModel
    
        delegate?.didTapRestaurantCell(cell: self)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: frame.width, height: 200)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 0.5
    }
    
    }
    
    我的
    菜单控制器的内部

     import UIKit
     import FBSDKLoginKit
    
     class RestaurantController: UICollectionViewController, UICollectionViewDelegateFlowLayout, SWRevealViewControllerDelegate, UISearchBarDelegate, RestaurantDelegate {
    
    var restaurantCell: RestaurantCell?
    
    private let restaurantCellId = "restaurantCellId"
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView?.backgroundColor = UIColor.qpizzaWhite()
        collectionView?.register(RestaurantCell.self, forCellWithReuseIdentifier: restaurantCellId)
    
    
        if self.revealViewController() != nil {
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "icon_menu_24dp").withRenderingMode(.alwaysOriginal), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    
    }
    
    // FIXME: issue with this...navigationcontroller is presenting, not pushing ontop of stack view
    func didTapRestaurantCell(cell: RestaurantCell) {
        print("Did Tap Restaurant Cell - Restaurant Controller")
    
        let layout = UICollectionViewFlowLayout()
        let controller = MenuController(collectionViewLayout: layout)
        navigationController?.pushViewController(controller, animated: true)
    
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: restaurantCellId, for: indexPath) as! RestaurantCell
        cell.delegate = self
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1
    }
    }
    
    import UIKit
    
    class MenuController: UICollectionViewController, UICollectionViewDelegateFlowLayout, SWRevealViewControllerDelegate {
    
    private let menuCellId = "menuCellId"
    
    var restaurant: RestaurantModel?
    var menuItems = [MenuItemsModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView?.backgroundColor = UIColor.qpizzaWhite()
        collectionView?.register(MenuCell.self, forCellWithReuseIdentifier: menuCellId)
        collectionView?.alwaysBounceVertical = true
    
        if self.revealViewController() != nil {
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "menu2-black-32").withRenderingMode(.alwaysOriginal), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    
        print("Restaurant Name:", restaurant?.name) // returns nil
        if let restaurantName = restaurant?.name {
            self.navigationItem.title = restaurantName
        }
    
        loadMenuItems()
    
    }
    
    func loadMenuItems() {
        print("Restaurant Id:", restaurant?.id) // returns nil
        if let restaurantId = restaurant?.id {
            print("RestaurantId:", restaurantId)
        }
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: menuCellId, for: indexPath) as! MenuCell
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let layout = UICollectionViewFlowLayout()
        let controller = MenuDetailsController(collectionViewLayout: layout)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 120)
    }
    
    }
    
    func didTapRestaurantCell(cell: RestaurantCell, withMenuController controller: MenuController) {
        print("Did Tap Restaurant Cell - Restaurant Controller")
        navigationController?.pushViewController(controller, animated: true)
    }
    
    我的
    菜单的内部

    import UIKit
    
    class MenuCell: BaseCell {
    
    let restaurantLabel: UILabel = {
        let label = UILabel()
        label.text = "Restaurant King"
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = .black
        label.numberOfLines = 0
        return label
    }()
    
    let mealImageView: UIImageView = {
        let iv = UIImageView()
        iv.image = #imageLiteral(resourceName: "button_chicken").withRenderingMode(.alwaysOriginal)
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    let mealDetailsLabel: UILabel = {
        let label = UILabel()
        label.text = "Grass fed grass, American cheese, and friez"
        label.font = UIFont.boldSystemFont(ofSize: 12)
        label.textColor = UIColor.qpizzaBlack()
        label.numberOfLines = 0
        return label
    }()
    
    let mealPriceLabel: UILabel = {
        let label = UILabel()
        label.text = "$12.00"
        label.font = UIFont.boldSystemFont(ofSize: 12)
        label.textColor = UIColor.qpizzaBlack()
        return label
    }()
    
    let sepereatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGray
        return view
    }()
    
    
    override func setupViews() {
        super.setupViews()
    
        backgroundColor = UIColor.qpizzaWhite()
    
        addSubview(restaurantLabel)
        addSubview(mealImageView)
        addSubview(mealDetailsLabel)
        addSubview(mealPriceLabel)
        addSubview(sepereatorView)
    
        _ = mealImageView.anchor(topAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: 14, leftConstant: 0, bottomConstant: 0, rightConstant: 12, widthConstant: 60, heightConstant: 60)
        _ = restaurantLabel.anchor(topAnchor, left: leftAnchor, bottom: nil, right: mealImageView.leftAnchor, topConstant: 14, leftConstant: 12, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 20)
        _ = mealDetailsLabel.anchor(restaurantLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: mealImageView.leftAnchor, topConstant: 12, leftConstant: 12, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 30)
        _ = mealPriceLabel.anchor(mealDetailsLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 10, leftConstant: 12, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 20)
        _ = sepereatorView.anchor(nil, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 0, leftConstant: 20, bottomConstant: 4, rightConstant: 20, widthConstant: 0, heightConstant: 1)
    
    
    }
    }
    
    protocol RestaurantDelegate {
        func didTapRestaurantCell(cell: RestaurantCell, withMenuController: MenuController)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Did Select Item - Restaurant Cell")
    
        let layout = UICollectionViewFlowLayout()
        let controller = MenuController(collectionViewLayout: layout)
        controller.restaurant = self.restaurants[indexPath.item]
        delegate?.didTapRestaurantCell(cell: self, withMenuController: controller)
    }
    

    简单看一下,首先声明一个正确类型的变量。但实际上,您必须执行赋值(=)才能将数据或类引用从一个类移动到下一个类

    func didTapRestaurantCell(cell: RestaurantCell) {
      print("Did Tap Restaurant Cell - Restaurant Controller")
    
      let layout = UICollectionViewFlowLayout()
      let controller = MenuController(collectionViewLayout: layout)
      navigationController?.pushViewController(controller, animated: true)
    
      // you need to set the restaurant attribute of your new 
      // controller
      let indexPath = indexPath(for: cell)
      controller.restaurant = self.restaurants[indexPath.item]
    }
    

    谢谢大家的评论,并帮助我弄明白这一点。我可以通过更改协议并将控制器作为参数传递来解决问题

    RestaurantCell
    中:

    import UIKit
    
    class MenuCell: BaseCell {
    
    let restaurantLabel: UILabel = {
        let label = UILabel()
        label.text = "Restaurant King"
        label.font = UIFont.boldSystemFont(ofSize: 16)
        label.textColor = .black
        label.numberOfLines = 0
        return label
    }()
    
    let mealImageView: UIImageView = {
        let iv = UIImageView()
        iv.image = #imageLiteral(resourceName: "button_chicken").withRenderingMode(.alwaysOriginal)
        iv.contentMode = .scaleAspectFill
        iv.clipsToBounds = true
        return iv
    }()
    
    let mealDetailsLabel: UILabel = {
        let label = UILabel()
        label.text = "Grass fed grass, American cheese, and friez"
        label.font = UIFont.boldSystemFont(ofSize: 12)
        label.textColor = UIColor.qpizzaBlack()
        label.numberOfLines = 0
        return label
    }()
    
    let mealPriceLabel: UILabel = {
        let label = UILabel()
        label.text = "$12.00"
        label.font = UIFont.boldSystemFont(ofSize: 12)
        label.textColor = UIColor.qpizzaBlack()
        return label
    }()
    
    let sepereatorView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.lightGray
        return view
    }()
    
    
    override func setupViews() {
        super.setupViews()
    
        backgroundColor = UIColor.qpizzaWhite()
    
        addSubview(restaurantLabel)
        addSubview(mealImageView)
        addSubview(mealDetailsLabel)
        addSubview(mealPriceLabel)
        addSubview(sepereatorView)
    
        _ = mealImageView.anchor(topAnchor, left: nil, bottom: nil, right: rightAnchor, topConstant: 14, leftConstant: 0, bottomConstant: 0, rightConstant: 12, widthConstant: 60, heightConstant: 60)
        _ = restaurantLabel.anchor(topAnchor, left: leftAnchor, bottom: nil, right: mealImageView.leftAnchor, topConstant: 14, leftConstant: 12, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 20)
        _ = mealDetailsLabel.anchor(restaurantLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: mealImageView.leftAnchor, topConstant: 12, leftConstant: 12, bottomConstant: 0, rightConstant: 10, widthConstant: 0, heightConstant: 30)
        _ = mealPriceLabel.anchor(mealDetailsLabel.bottomAnchor, left: leftAnchor, bottom: nil, right: rightAnchor, topConstant: 10, leftConstant: 12, bottomConstant: 10, rightConstant: 10, widthConstant: 0, heightConstant: 20)
        _ = sepereatorView.anchor(nil, left: leftAnchor, bottom: bottomAnchor, right: rightAnchor, topConstant: 0, leftConstant: 20, bottomConstant: 4, rightConstant: 20, widthConstant: 0, heightConstant: 1)
    
    
    }
    }
    
    protocol RestaurantDelegate {
        func didTapRestaurantCell(cell: RestaurantCell, withMenuController: MenuController)
    }
    
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        print("Did Select Item - Restaurant Cell")
    
        let layout = UICollectionViewFlowLayout()
        let controller = MenuController(collectionViewLayout: layout)
        controller.restaurant = self.restaurants[indexPath.item]
        delegate?.didTapRestaurantCell(cell: self, withMenuController: controller)
    }
    
    餐厅控制器中

     import UIKit
     import FBSDKLoginKit
    
     class RestaurantController: UICollectionViewController, UICollectionViewDelegateFlowLayout, SWRevealViewControllerDelegate, UISearchBarDelegate, RestaurantDelegate {
    
    var restaurantCell: RestaurantCell?
    
    private let restaurantCellId = "restaurantCellId"
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView?.backgroundColor = UIColor.qpizzaWhite()
        collectionView?.register(RestaurantCell.self, forCellWithReuseIdentifier: restaurantCellId)
    
    
        if self.revealViewController() != nil {
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "icon_menu_24dp").withRenderingMode(.alwaysOriginal), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    
    }
    
    // FIXME: issue with this...navigationcontroller is presenting, not pushing ontop of stack view
    func didTapRestaurantCell(cell: RestaurantCell) {
        print("Did Tap Restaurant Cell - Restaurant Controller")
    
        let layout = UICollectionViewFlowLayout()
        let controller = MenuController(collectionViewLayout: layout)
        navigationController?.pushViewController(controller, animated: true)
    
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: restaurantCellId, for: indexPath) as! RestaurantCell
        cell.delegate = self
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 1
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: view.frame.height)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        return 1
    }
    }
    
    import UIKit
    
    class MenuController: UICollectionViewController, UICollectionViewDelegateFlowLayout, SWRevealViewControllerDelegate {
    
    private let menuCellId = "menuCellId"
    
    var restaurant: RestaurantModel?
    var menuItems = [MenuItemsModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
    
        collectionView?.backgroundColor = UIColor.qpizzaWhite()
        collectionView?.register(MenuCell.self, forCellWithReuseIdentifier: menuCellId)
        collectionView?.alwaysBounceVertical = true
    
        if self.revealViewController() != nil {
            navigationItem.leftBarButtonItem = UIBarButtonItem(image: #imageLiteral(resourceName: "menu2-black-32").withRenderingMode(.alwaysOriginal), style: .plain, target: self.revealViewController(), action: #selector(SWRevealViewController.revealToggle(_:)))
            self.view.addGestureRecognizer(self.revealViewController().panGestureRecognizer())
        }
    
        print("Restaurant Name:", restaurant?.name) // returns nil
        if let restaurantName = restaurant?.name {
            self.navigationItem.title = restaurantName
        }
    
        loadMenuItems()
    
    }
    
    func loadMenuItems() {
        print("Restaurant Id:", restaurant?.id) // returns nil
        if let restaurantId = restaurant?.id {
            print("RestaurantId:", restaurantId)
        }
    }
    
    override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: menuCellId, for: indexPath) as! MenuCell
        return cell
    }
    
    override func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return 3
    }
    
    override func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let layout = UICollectionViewFlowLayout()
        let controller = MenuDetailsController(collectionViewLayout: layout)
        navigationController?.pushViewController(controller, animated: true)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        return CGSize(width: view.frame.width, height: 120)
    }
    
    }
    
    func didTapRestaurantCell(cell: RestaurantCell, withMenuController controller: MenuController) {
        print("Did Tap Restaurant Cell - Restaurant Controller")
        navigationController?.pushViewController(controller, animated: true)
    }
    

    我已经有一段时间没有使用CollectionView了,但是为什么
    didSelectItemAtIndexPath
    在单元格中而不是控制器中?显然,您还创建了两个不同的
    MenuController
    实例,一个在单元格的
    didSelectItemAtIndexPath
    方法中,另一个在
    RestaurantDelegate
    didTapRestaurantCell
    方法中,该方法不设置
    restaurant
    属性。在第一个示例中,您设置了RestaurantCell,但该控制器实例只是挂起了一点&没有使用-它不是导航控制器推送的实例。
    didSelectItemAtIndexPath'位于
    RestaurantCell`内,因为我在
    RestaurantCell
    内有一个嵌套的UICollectionViewController。您的说法是正确的,我正在创建两个不同的
    MenuController
    。我不知道如何在不创建新实例的情况下引用
    RestaurantCell
    内部的
    MenuController
    ,你有什么建议吗?我需要
    RestaurantController
    中的
    MenuController
    的第一个实例将导航控制器推到堆栈上,然后推到
    MenuController
    中的第二个实例
    RestaurantCell
    获取我的
    MenuController
    @a2b123上的
    restaurant
    (它引用了我的餐厅模型)的引用为什么要将单元格传递回controller类?为什么不把数据传回去,然后把数据分离出来呢?您的协议应该保存类型为restaurants(这是您的模型数组)的数据,当您点击一个单元格时,您可以获取该单元格的数据,并将其交给协议,它会将其委托回控制器类,然后您的segue会将其交给另一个类。@a2b123我的意思是委托部分,您在其中传递单元格,只需将点击的单元格数据传回,当调用返回到控制器类时,您可以将数据发送到第二类。如何在我的
    RestaurantController中访问
    餐厅
    。我已经在我的
    RestaurantCell
    中声明了该变量。另外,如何在我的
    RestaurantController
    中访问
    indexath.item
    @阿哈尔。我尝试过多次使用这种方法,但我不确定如何访问这些片段。这还不够,您必须将单元格映射到
    数组中的元素。我看不出您首先是如何填充单元格的返回指定单元格的索引路径。
    很抱歉,我没有遵循您的全部代码。。。但缺少的是在创建MenuController之后对其属性值的实际赋值(=)。声明正确类型的属性是必要的第一步,下一步是为类的实际实例赋值