如何重新加载整个UIViewController页面

时间:2020-06-01 21:19:20

标签: ios swift

从另一个UIViewController返回后,我想重新加载UIViewController

在我的ReviewController中,我有这段代码可以打开WriteReviewController

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

我在WriteReviewController中使用

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

返回到ReviewController。 我想使ReviewController重新加载页面,以便新的评论可以显示在页面上。预先谢谢你。

1 个答案:

答案 0 :(得分:2)

首先,它是UIViewController,而不是UIController。 其次-取决于是否必须从您的WriteReviewController传回数据

  1. 如果是,则需要创建一个带有功能的协议以传回新的评论。

  2. 如果您只想为ReviewController重新加载数据模型,则可以简单地在viewWillAppear(animated:)继承的UIViewController函数中执行重新加载逻辑。

让我知道您需要哪种方式的进一步帮助,我可以帮助您编写代码。


编辑:我很确定答案1是正确的,所以这里有一些帮助:

// 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)
    }
}