我有两个视图控制器。主要和临时之一。第二个在不同的屏幕上执行一个操作(由pushViewController
调用),然后我弹出(popViewController
),并希望呈现返回值为String。传递popViewController之前没有segue和storyboard的数据
我试过使用协议,但它是零。
这里是我的代码:
SecondVC.swift:
protocol ValueDelegate {
func append(_ text: String)
}
class SecondViewController: UIViewController{
var delegate: ValueDelegate!
...
...
private func function(){
if let delegate = self.delegate{
delegate.append(value.stringValue)
}
navigateBack()
}
private func navigateBack(){
if let navigation = self.navigationController{
navigation.popViewController(aniamted: true)
}
}
MainVC.swift:
class MainViewController: UIViewController, ValueDelegate {
var secondVC = SecondViewController()
...
func append(_ value: String) {
textField.text?.append(barcode)
}
...
override func viewDidLoad(){
super.viewDidLoad()
self.secondVC.delegate = self
}
}
你从第二个回到主要的权利? –
你可以在这里查看示例:https://stackoverflow.com/questions/45534199/calling-delegate-function-from-appdelegate-not-working –