gpt4 book ai didi

ios - 如何在使用 Swift 关闭 ViewController 期间将值从 ViewController B 传递到 ViewController A?

转载 作者:搜寻专家 更新时间:2023-10-31 23:06:02 27 4
gpt4 key购买 nike

<分区>

在我的场景中,我试图在关闭 View Controller 期间将值从 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)
}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com