我的情况是,我试图在关闭视图控制器的过程中将值从传递ViewController B
给ViewController A
。在这里,我使用了以下代码,但无法获取中的值ViewController A
。
ViewController B
// protocol used for sending data back
protocol isAbleToReceiveData {
func pass(data: String) //data: string is an example parameter
}
// Making this a weak variable so that it won't create a strong reference cycle
var delegate: isAbleToReceiveData?
override func viewWillDisappear(_ animated: Bool) {
super.viewWillDisappear(true)
self.delegate?.pass(data: "someData") //call the func in the previous vc
}
@IBAction func Click_action(_ sender: Any) {
self.dismiss(animated: false, completion: nil)
self.delegate?.pass(data: "someData")
}
ViewController A
class MyViewController: UIViewController, isAbleToReceiveData {
func pass(data: String) {
print("USER: \(data)")
}
}
// MARK: FromTouch Action
@objc func fromTouchTapped(_ sender: UITapGestureRecognizer) {
let storyboard = UIStoryboard(name: "Main", bundle: nil)
let viewController = storyboard.instantiateViewController(withIdentifier: "ViewControllerB")
viewController.modalTransitionStyle = .crossDissolve
let navController = UINavigationController(rootViewController: viewController)
present(navController, animated: true, completion: nil)
}
一切都对,您错过了ViewControllerA
当下指派的代表ViewControllerB
。
if let VC_B = self.storyboard?.instantiateViewController(withIdentifier: "ViewControllerB") as? ViewControllerB{
VC_B.delegate = self
VC_B.modalTransitionStyle = .crossDissolve
self.present(VC_B, animated: true, completion: nil)
}
注意
让viewController = self.storyboard?.instantiateViewController(withIdentifier:“ ViewControllerB”)为?ViewControllerB
的
让viewController = self.storyboard?.instantiateViewController(withIdentifier:“ ViewControllerB”)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句