我制作的模态视图会在用户触摸按钮时显示。目前,我已经展示了模态,我可以使用该模态界面。
现在,当按下模态中的按钮时,我不想忽略模态,然后在它之后从另一个视图的父母那里做一个segue。
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
。在那一刻,我希望呈现模态的父视图控制器捕获模态上的文本并关闭模态。我该怎么办?
答案 0 :(得分:0)
@IBAction func saveSummary(_ sender: Any) {
print("SummaryTV: \(summaryTV.text)")
self.presentingViewController.dismissViewControllerAnimated(false,
completion: nil)
}
对于通信,您可以设置一个弱委托来呈现将实现协议的VC:
protocol SummaryResult {
func saveResult(summary: String)
}