gpt4 book ai didi

ios - 将数据从容器 View 传递到 MainViewController

转载 作者:搜寻专家 更新时间:2023-10-31 22:54:33 24 4
gpt4 key购买 nike

我有这个代码:

MainViewControler:
class MainViewControler: UIViewController, ContainerToMaster {
@IBOutlet weak var systemContainerView: UIView!
@IBOutlet weak var favoriteProductBtn2: UIButton!
weak var containerViewController: Calculator1ViewController?

func changeBtn() {
favoriteProductBtn2.isHidden = true
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
}
}


CallculatorViewController:
@IBOutlet weak var containerView: UIView!

class CallculatorViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
}
}


Calculator1ViewController:
protocol ContainerToMaster {
func changeBtn()
}

class Calculator1ViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
containerToMaster?.changeBtn()
}
}

我有这样一个应用布局:

  1. 主视图 = MainViewControler。在 MainViewControler 中,我有一个 containerView (systemContainerView),MainViewControler 位于其中

  2. CallculatorViewController 中,我有 Calculator1ViewController 所在的下一个 containerView。

当进入 Calculator1ViewController 时,函数:containerToMaster?.changeBtn() 应该启动(它应该在 MainViewControler 中工作)。

问题是 - 这个函数不起作用:(

有人知道怎么解决吗?

最佳答案

包含的 viewController 嵌入了 embed segue。您需要覆盖 prepare(for:sender) 并将 self 设置为目标 viewController 中的 containerToMaster 委托(delegate)。由于您有一个容器 View 嵌入到另一个容器 View 中,因此您的情况很复杂,因此您需要设置两个委托(delegate)并传回按钮回调:

protocol ContainerToMaster {
func changeBtn()
}


class MainViewControler: UIViewController, ContainerToMaster {
@IBOutlet weak var systemContainerView: UIView!
@IBOutlet weak var favoriteProductBtn2: UIButton!
weak var containerViewController: Calculator1ViewController?

func changeBtn() {
favoriteProductBtn2.isHidden = true
print("XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX")
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? CallculatorViewController {
destVC.containerToMaster = self
}
}
}

class CallculatorViewController: UIViewController, ContainerToMaster {
weak var containerToMaster: ContainerToMaster?

override func viewDidLoad() {
super.viewDidLoad()
}

func changeBtn() {
// pass the button call back
containerToMaster?.changeBtn()
}

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destVC = segue.destination as? Calculator1ViewController {
destVC.containerToMaster = self
}
}
}

class Calculator1ViewController: UIViewController {
weak var containerToMaster: ContainerToMaster?

override func viewDidLoad() {
super.viewDidLoad()
containerToMaster?.changeBtn()
}
}

关于ios - 将数据从容器 View 传递到 MainViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51322124/

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