如何在 SWIFT 中重绘我的视图?

How to redraw my view in SWIFT?

在我的 iPad 应用程序中,我有一个 UIViewController,带有一个打开 modalView 的按钮。

@IBAction func showPostCommentViewController(sender: AnyObject){

    let modalView = UIStoryboard(name: "Main", bundle: nil).instantiateViewControllerWithIdentifier("PostCommentViewController") as! PostCommentViewController
    modalView.modalTransitionStyle = UIModalTransitionStyle.CoverVertical
    modalView.modalPresentationStyle = UIModalPresentationStyle.FormSheet
    modalView.delegate=self
    self.presentViewController(modalView, animated: true, completion: nil)
}

当我使用 dismissViewControllerAnimated 关闭 modalView 时,我想要 "refresh" 我的视图控制器(因为我添加了新内容)。但是由于模态视图是 "formsheet" 样式,因此不会调用 viewDidAppear 或 viewWillAppear。

我尝试使用 setNeedsDisplay,但它不起作用。

我不知道怎么办。

这将是委托模式的完美用例。

1) 在 PostCommentViewController 内定义一个协议。

protocol PostCommentVCInformationDelegate {
    func hasDismissedPostCommentViewController(controller:PostCommentViewController)
}

2) 在PostCommentViewController

中设置一个委托变量
var delegate: PostCommentVCInformationDelegate?

3)当你解散PostCommentViewController时,你会调用delegate?.hasDismissedPostCommentViewController(self)

这会将信息发送回演示者 VC。

4) 现在我们的呈现视图控制器符合这个协议。

class ViewController: UIViewController, PostCommentVCInformationDelegate

5) 呈现模式视图时:

modalView.delegate = self

6) 最后,我们实现:

func hasDismissedPostCommentViewController(controller: PostCommentViewController) {
    //Update
}