【问题标题】:How to dismiss formSheet Modal from ParentViewController?如何从 ParentViewController 中关闭 formSheet 模态?
【发布时间】:2018-01-15 15:46:57
【问题描述】:

我正在制作一个模式视图,当用户触摸按钮时会显示该视图。此刻,我已经展示了模态,我可以使用那个模态界面了。

现在我不想在按下模态框内的按钮时关闭模态框,然后从它的父视图转到另一个视图。

let summary = SummaryViewController(nibName: "SummaryViewController", bundle: nil)
summary.preferredContentSize = CGSize(width: 800, height: 300)
summary.modalPresentationStyle = UIModalPresentationStyle.formSheet
self.present(summary, animated: true, completion: nil)

SummaryViewController 代码如下:

import UIKit

class SummaryViewController: UIViewController, UITextViewDelegate {

  @IBOutlet weak var summaryTV: UITextView!

  override func viewDidLoad() {
    super.viewDidLoad()
    summaryTV.delegate = self
    summaryTV.textColor = UIColor.lightGray
    summaryTV.text = "Escriba el resumen de la visita"
  }

  override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
  }


  func textViewDidBeginEditing(_ textView: UITextView) {
    if textView.textColor ==  UIColor.lightGray {
      textView.text = nil
      textView.textColor = UIColor.black
    }
  }
  func textViewDidEndEditing(_ textView: UITextView) {
    if textView.text.isEmpty {
      textView.textColor = UIColor.lightGray
      textView.text = "Escriba el resumen de la visita"
    }
  }

  @IBAction func saveSummary(_ sender: Any) {
    print("SummaryTV: \(summaryTV.text)")
  }

  // MARK: Segues
  // Overrided functions for segues. Ordered according to segue flow.
  override func shouldPerformSegue(withIdentifier identifier: String, sender: Any?) -> Bool {
    // If false, segue cancelled. Segue identifier is a parameter of the function.
    // If you use function 'performSegue()' flow do not pass this function.
    if !Reachability.isConnectedToNetwork() {
      CommonAlerts.inetAlert(vc: self)
      return false
    }
    return true
  }
  override func prepare(for segue: UIStoryboardSegue, sender: Any?) {

  }
}

因此,当用户按下模态视图内的按钮时,将触发 IBAction saveSummary。在那一刻,我希望呈现模态的父视图控制器捕获模态上的文本并关闭模态。我该怎么做?

【问题讨论】:

  • 在同一个 ParentViewController 中,我有一个显示为 UIModalPresentationStyle.popover 的菜单,我将演示文稿委托给 ParentViewController。有了这个,我可以使用函数popoverPresentationControllerDidDismissPopover 管理解雇。出于某种原因,我不明白我不能在模态演示formSheet 中使用相同的功能。
  • 您可以在您的模态类中创建一个协议,并让您的父级遵守该协议。因此,当模式被解除时,只需调用您的委托方法,该方法将触发父级中的委托功能。所以在那个协议方法中你可以做任何你想做的事情。

标签: ios swift swift3


【解决方案1】:
@IBAction func saveSummary(_ sender: Any) {
    print("SummaryTV: \(summaryTV.text)")

    self.presentingViewController.dismissViewControllerAnimated(false,
          completion: nil)
  }

至于通信,您可以设置一个弱委托来展示将实现协议的 VC:

protocol SummaryResult {
   func saveResult(summary: String)
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-14
    相关资源
    最近更新 更多