简体   繁体   English

如何从ParentViewController中退出formSheet Modal?

[英]How to dismiss formSheet Modal from ParentViewController?

I'm making a modal view that presents when the user touch up on a button. 我正在制作一个模态视图,当用户触摸按钮时显示。 At the moment, I already show the modal and I can use that modal interface. 目前,我已经显示了模态,并且可以使用该模态界面。

Now I wan't to dismiss the modal when a button inside the modal is pressed and after that do a segue from it's parent to another view. 现在,当我按下模态内部的按钮时,我将不关闭模态,然后从模态的父视图切换到另一个视图。

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 have the following code: 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?) {

  }
}

So when the user press the button inside the modal view, the IBAction saveSummary is triggered. 因此,当用户按下模式视图内的按钮时,将触发IBAction saveSummary In that moment I want the parent view controller who present the modal to capture the text on the modal and dismiss the modal. 在那一刻,我希望呈现模式的父视图控制器捕获模式上的文本并关闭模式。 How can I do it? 我该怎么做?

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

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

As for communication, you can setup a weak delegate to presenting VC which will implement a protocol: 对于通信,您可以设置一个弱代表来呈现VC,以实现协议:

protocol SummaryResult {
   func saveResult(summary: String)
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从parentViewController的containerView中的childViewController中消除键盘? - How to dismiss keyboard from childViewController which is in containerView in parentViewController? 如何在“ parentViewController”中显示模式视图? - How to present modal view in “parentViewController”? 如何关闭当前视图控制器和父视图控制器? - How to dismiss current view controller and parentviewcontroller? 从iPad上的模态FormSheet呈现MFMailComposeViewController? - Present MFMailComposeViewController from modal FormSheet on iPad? 如何从右到左关闭模态ViewController? - How to dismiss modal ViewController from right to left? 如何从appdelegate呈现和消除模式视图? - How to present and dismiss modal view from appdelegate? 如何从UIAlertcontroller中消除模式ViewController - How to dismiss modal ViewController from UIAlertcontroller 如何从子视图访问ParentViewController方法? - How to access ParentViewController method from subview? 如何在不关闭模式的情况下关闭从modalViewController推送的viewController? - How to dismiss a viewController pushed from a modalViewController without dismissing the modal? 从模态中取消视图控制器 - Dismiss View Controller from Modal
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM