覆盖func prepare(对于segue:UIStoryboardSegue,发件人:任何?)不起作用

时间:2018-06-25 08:42:19

标签: ios swift firebase segue

我有一个表视图控制器,其作用类似于图像的新闻提要,其中每个图像都已从firebase中检索到。我希望能够在每个图像上进行点击,然后对另一个具有表格视图的视图控制器进行选择,该表格视图具有其自己的点击图像的唯一数据。 使用我拥有的代码,override func prepare(for:sender)没有被调用,也没有错误。轻按图片但未执行Show Segue时,控制台中将打印唯一的帖子ID。

这是我在HomeTableViewCell中的代码:

protocol HomeTableViewCellDelegate {
    func goToSiteVC(postId: String)

}

class HomeTableViewCell: UITableViewCell {
    @IBOutlet weak var siteImageView: UIImageView!
    var delegate: HomeTableViewCellDelegate?

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)
        let tapGesture = UITapGestureRecognizer(target: self, action: #selector(self.siteImageView_TouchUpInside))
        siteImageView.addGestureRecognizer(tapGesture)
        siteImageView.isUserInteractionEnabled = true
        // Configure the view for the selected state
    }

    @objc func siteImageView_TouchUpInside() {
        print("siteImageView_TouchUpInside")
        print(post?.id)
        if let id = post?.id {
            delegate?.goToSiteVC(postId: id)
        }
    }
}

这是我在HomeViewController中的代码:

class HomeViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var posts = [Post]()
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        loadPosts()
    }

        override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "SiteSegue" {
                let siteVC = segue.destination as! SiteViewController
                let postId = sender as! String
                siteVC.postId = postId
                print("segue")
            }
        }
    }


extension HomeViewController: HomeTableViewCellDelegate {
    func goToSiteVC(postId: String) {
        performSegue(withIdentifier: "SiteSegue", sender: postId)
    }
}

1 个答案:

答案 0 :(得分:1)

该函数应不在viewDidLoad范围内,并且必须在类范围内

class HomeViewController: UIViewController {

    @IBOutlet weak var tableView: UITableView!
    var posts = [Post]()
    override func viewDidLoad() {
        super.viewDidLoad()

        tableView.dataSource = self
        loadPosts()



    }

   override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
            if segue.identifier == "SiteSegue" {
                let siteVC = segue.destination as! SiteViewController
                let postId = sender as! String
                siteVC.postId = postId
                print("segue")
            }
        }

}