Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

ios - Dismiss my view controller but "prepare" the view controller underneath

I have a modally-presented ViewController that has a UITableView inside it. When the user clicks "Days", it shows this view controller (modally). Here's my Storyboard:

enter image description here

enter image description here

What I want to do is, when I click one of the cells in the table view in the modal, I want to dismiss the modal, but also load new data in the root view controller.

How can I have access to the root view controller from a segued view controller?

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
    self.dismiss(animated: true, completion: nil)
}

// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.destination as! DayView
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.day = days![indexPath.row]
    }
}

I want something like what I've written above, but while my modal does get dismissed, but what I want is for somehow the code in the prepare function to fire when a TableViewCell is clicked.

question from:https://stackoverflow.com/questions/65932247/dismiss-my-view-controller-but-prepare-the-view-controller-underneath

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

you are in the right path,you need to add two more functions 1. unwindsegue and one this is not destination its source so you need change to source from destination.

 // MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
    let destinationVC = segue.source as! DayView
    if let indexPath = tableView.indexPathForSelectedRow {
        destinationVC.day = days![indexPath.row]
    }
}

for Reference : How to Pass Data in an Unwind Segue


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...