如何修复显示灰色屏幕的segue



我想创建一个从FirstViewController到DetailViewController的分段,但当我编写时

let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailVC, animated: true, completion: nil)

,它所呈现的只是一个灰色/透明的屏幕。我该怎么解决这个问题?

func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return posts.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "PostCell", for: indexPath) as! PostTableViewCell

cell.typeLabel.text = "Name: (posts[indexPath.row].typeOfFood)"
cell.titleLabel.text = "Title: (posts[indexPath.row].title)"
cell.locationLabel.text = "Location: (posts[indexPath.row].location)"
cell.nameLabel.text = "Name: (posts[indexPath.row].name)"
return cell

}

func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
//        print("Select Row (indexPath.row)")
var result = indexPath.row
tableView.deselectRow(at: indexPath, animated: true)
//        self.performSegue(withIdentifier: "showDetail", sender: nil)
let detailVC = DetailViewController()
detailVC.result = indexPath.row
print(detailVC.result)
self.present(detailVC, animated: true, completion: nil)

}
} ```

问题在于短语DetailViewController()。这并不是你在故事板中设计的局部视图控制器。

相反,给您在情节提要中设计的视图控制器一个标识符,并通过调用以下方法向情节提要询问该视图控制器:

https://developer.apple.com/documentation/uikit/uistoryboard/1616214-instantiateviewcontroller

现在将视图控制器向下投射到DetailViewController,然后从那里开始。

最新更新