如何在 SWIFT 中重绘我的视图? [英] How to redraw my view in SWIFT?
问题描述
在我的 iPad 应用程序上,我有一个 UIViewController 和一个按钮,可以打开一个 modalView.
On my iPad app, I have a UIViewController with a button that open a 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 时,我想刷新"我的视图控制器(因为我添加了新内容).但由于模态视图是表单"样式,因此不会调用 viewDidAppear 或 viewWillAppear.
When I close the modalView with dismissViewControllerAnimated, I would like "refresh" my view controller (because I added new content). But as the modal view is a "formsheet" style, viewDidAppear or viewWillAppear aren't called.
我尝试使用 setNeedsDisplay,但它不起作用.
I tried to use setNeedsDisplay, but it doesn't work.
我不知道该怎么办.
推荐答案
这将是委托模式的完美用例.
This would be a perfect use case for the delegate pattern.
1) 在 PostCommentViewController
中定义一个协议.
1) define a protocol within PostCommentViewController
.
protocol PostCommentVCInformationDelegate {
func hasDismissedPostCommentViewController(controller:PostCommentViewController)
}
2) 在 PostCommentViewController
var delegate: PostCommentVCInformationDelegate?
3) 当您关闭 PostCommentViewController
时,您将调用 delegate?.hasDismissedPostCommentViewController(self)
3) When you dismiss PostCommentViewController
, you will call delegate?.hasDismissedPostCommentViewController(self)
这会将信息发送回呈现的 VC.
This will send information back to the presenting VC.
4) 现在我们的呈现视图控制器符合这个协议.
4) Now we have our presenting View Controller conform to this protocol.
class ViewController: UIViewController, PostCommentVCInformationDelegate
5) 当呈现模态视图时:
5) When presenting the modal View:
modalView.delegate = self
6) 最后,我们实现:
6) Finally, we implement:
func hasDismissedPostCommentViewController(controller: PostCommentViewController) {
//Update
}
这篇关于如何在 SWIFT 中重绘我的视图?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!