我正在尝试使用可重用函数将数据从弹出式VC传递到父VC,以呈现弹出式窗口并获取结果。我尝试在弹出窗口中将闭包方法与函数变量一起使用,出现错误“ UIViewController没有成员'onSave'(函数变量)。任何帮助,将不胜感激。谢谢
我使用了委托和segues,但想避免使用,因此可以在弹出窗口中显示一个功能并收集许多实例的选择。
// Parent VC
class a2ExpenseAddViewController: UIViewController {
@IBAction func selectEtype(_ sender: Any) {
popTitle = "Expense Types"//global var
popMessage = "Select type below or add new" // global var
menuPopup(choices: eTypes){ result in guard let result = else{
return
}
self.eType.setTitle(result, for: .normal)
print("popChoice=",result)
}
}
extension UIViewController {
func menuPopup (choices: [String], completion: @escaping (_ choice: String?) -> ()) {
let main = UIStoryboard(name: "Main", bundle: nil)
let goto = main.instantiateViewController(withIdentifier: "popup")
popChoices = choices
// Error below here - UIViewController has no member 'onSave'
goto.onSave = { (data: String) -> () in
choice = data
}
self.present(goto, animated: true, completion: nil)
}
//Popup VC
PopupViewController: UIViewController,.. // storyboard id is 'popup'
var onSave: ((_ data: String) -> ())?
@IBAction func add(_ sender: Any) {
popChoice = choice.text!
onSave?(popChoice)
dismiss(animated: true) }
我希望通过onSave(data)将结果分配给按钮标题。 我收到一个错误“ UIViewController没有成员'onSave'(函数变量)。
答案 0 :(得分:0)
实例化后将ViewController设置为特定的类类型。
let goto = main.instantiateViewController(withIdentifier: "popup") as? PopupViewController
那么您应该可以做到
goto.onSave = { (data: String) -> () in
choice = data
}