我正在使用 swift4
,我将 View Controller 模态地呈现为弹出窗口,并希望在关闭当前弹出窗口时将数据传回之前的 View Controller ,这是第一个 View Controller :
import UIKit
class SearchResultViewController: UIViewController, UIPopoverPresentationControllerDelegate, PopoupDelegate {
@IBOutlet var errorLable: UILabel!
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if (segue.identifier == "pop") {
let dest = segue.destination
if let pop = dest.popoverPresentationController {
dest.preferredContentSize = CGSize(width: self.view.frame.size.height - 20, height: 500)
pop.delegate = self
}
}
}
func adaptivePresentationStyle(for controller: UIPresentationController) -> UIModalPresentationStyle {
return .none
}
func popupValueSelected(value: String) {
print("value: ", value)
self.errorLable.text = value
}
}
这是弹出代码:
import UIKit
class SearchPopoverViewController: UIViewController {
@IBOutlet var cityLable: UILabel!
var delegate: PopoupDelegate?
@IBAction func closeButtonAction(_ sender: Any) {
self.delegate?.popupValueSelected(value: "hiiiiii from the other side")
self.dismiss(animated: false, completion: nil)
}
}
这是PopoupDelegate
import Foundation
protocol PopoupDelegate {
func popupValueSelected(value: String)
}
当我点击弹出窗口关闭按钮时,弹出窗口应该消失并且 SearchResultViewController
中的 errorLable
应该将它的文本更改为新传递的文本,但是什么都没有发生,只有弹出窗口关闭,
我该怎么办?
最佳答案
你委托(delegate)的函数是错误的,SearchResultViewController
中的函数内部函数:
func popupValueSelected(value: String) {
func popupValueSelected(value: String) {
print("value: ", value)
self.errorLable.text = value
}
}
应该是:
func popupValueSelected(value: String) {
print("value: ", value)
self.errorLable.text = value
}
编辑问题后:
您从未将任何实例分配给委托(delegate)变量。 尝试将委托(delegate)变量名称更改为 popoUpDelegate。
pop.delegate
适用于 UIPopoverPresentationControllerDelegate
而不是您的委托(delegate)。这个函数应该是这样的:
if (segue.identifier == "pop") {
if let dest = segue.destination.popoverPresentationController as? SearchPopoverViewController {
dest.preferredContentSize = CGSize(width: self.view.frame.size.height - 20, height: 500)
dest.popoUpDelegate = self
}
}
}
你的 SearchPopoverViewController
看起来像这样:
class SearchPopoverViewController: UIViewController {
@IBOutlet var cityLable: UILabel!
var popoUpDelegate: PopoupDelegate?
@IBAction func closeButtonAction(_ sender: Any) {
self.popoUpDelegate?.popupValueSelected(value: "hiiiiii from the other side")
self.dismiss(animated: false, completion: nil)
}
}
关于ios - swift4:无法将值从当前弹出窗口传递到上一个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50719195/