WPN
WPN

Reputation: 63

How to reload the whole UIViewController page

I want to reload my UIViewController after I return back from another UIViewController

In my ReviewController I have this code to open the WriteReviewController

let newViewController = WriteReviewController()
navigationController?.present(newViewController, animated: true, completion: nil)

In my WriteReviewController, I use

self.dismiss(animated: true, completion: nil) 

to return back to the ReviewController. I want to make the ReviewController to reload page so that the new review can show up on the page. Thank you in advance.

Upvotes: 1

Views: 64

Answers (1)

emmics
emmics

Reputation: 1063

First of all, it's UIViewController, not UIController. Secondly - it depends if you have to pass data back from your WriteReviewController or not.

  1. If yes, you need to create a protocol with a function to pass back the new review.

  2. If you simply want to reload your data model for the ReviewController, you can simply do your reloading logic in viewWillAppear(animated:) function, inherited by UIViewController.

Let me know which way you need further help with and I can help you with writing the code.


EDIT: I'm pretty sure answer 1 is correct, so here's some help:

// assuming you have a struct like this for your Review data model
struct Review {
    var reviewText: String
    var author: String
}

// add this code to your ReviewController
protocol WriteReviewDelegate: class {
    func newReviewHasBeenWritten(_ review: Review)
}
class ReviewController: UIViewController {
    // ...
}
// make that ReviewController conform to this class
extension ReviewController: WriteReviewDelegate {
    func newReviewHasBeenWritten(_ review: Review) {
        // save the review to your model here
        // afterwards, update your UI to show the new review
    }
}

// add the following code to your WriteReviewController
class WriteReviewController {
    weak var delegate: WriteReviewDelegate?

    func saveReview() {
        // called when the user wants to  save a new review
        // notify the delegate (your ReviewController), that there's a new Review
        self.delegate?.newReviewHasBeenWritten(self.review)
        // then dismiss it, for example
        self.dismiss(animated: true, completion: nil)
    }
}

Upvotes: 2

Related Questions