【问题标题】:How to redraw my view in SWIFT?如何在 SWIFT 中重绘我的视图?
【发布时间】:2015-07-05 17:06:29
【问题描述】:

在我的 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 时,我想“刷新”我的视图控制器(因为我添加了新内容)。但由于模态视图是“表单”样式,因此不会调用 viewDidAppear 或 viewWillAppear。

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

我不知道该怎么做。

【问题讨论】:

  • 你想要发生什么? setNeedsDisplay 可能会更新布局和类似的东西,但标签等的内容不会改变,你必须明确地改变它们。你到底想做什么?
  • 您可以在“PostCommentViewController”创建一个委托,该委托将在视图本身消失时创建。然后在被调用者处,您可以定义要使用特定委托执行的任何操作

标签: ios swift uiviewcontroller setneedsdisplay uimodalpresentationstyle


【解决方案1】:

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

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
}

【讨论】:

  • 我不熟悉协议。非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
  • 1970-01-01
相关资源
最近更新 更多