我的场景,我试图传递值ViewController B
to ViewController A
在关闭视图控制器期间。这里我使用了下面的代码,但我无法获取其中的值ViewController A
.
视图控制器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")
}
视图控制器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)
}
Note
让视图控制器=
self.storyboard?.instantiateViewController(withIdentifier:
“ViewControllerB”)作为?视图控制器B
代替
让视图控制器=
self.storyboard?.instantiateViewController(withIdentifier:
“视图控制器B”)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)