gpt4 book ai didi

ios - 使用默认后退按钮时 Unwind 方法不起作用

转载 作者:行者123 更新时间:2023-11-28 06:15:57 25 4
gpt4 key购买 nike

我有如下三个 View Controller

enter image description here

我在 viewcontroller1 中写了 unwind 方法,并尝试在 viewcontroller2 和 viewcontroller3 unwind 到 viewcontroller1 时接收一些数据。

@IBAction func unwindToViewController1(segue: UIStoryboardSegue) {
print("1")
main_content = (segue.source as! MainContentViewController).main_content
}

@IBAction func unwindToViewController2(segue: UIStoryboardSegue) {
print("2")
detailed_content = (segue.source as! SupplementContentViewController).supplement
}

并且已经为 Controller 2 和 Controller 3 设置了 exit unwind segue。 enter image description here

但为什么 unwindToViewController 方法从未被正确调用?我认为当我点击系统自动创建的按钮时应该调用它们。

button

最佳答案

我通过使用委托(delegate)而不是展开来解决这个问题。 Delegate pattenr 是解决这个问题的一种更明确的方式。通过创建一个名为 myDelegate 的协议(protocol)

protocol myDelegate {
func updateMaincontent(main_content : String)
func updateSupplement(supplement: String)
}

并在第二个 View Controller 中创建一个委托(delegate)实例

var delegate: myDelegate?

然后在第一个 View Controller 中,使类扩展 myDelegate 并在 func prepare(for segue: UIStoryboardSegue, sender: Any?) 方法

override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if let destinationViewController = segue.destination as? MainContentViewController {
destinationViewController.main_content = self.main_content
destinationViewController.delegate = self
}
else if let destinationViewController = segue.destination as? SupplementContentViewController {
destinationViewController.supplement = self.detailed_content
destinationViewController.delegate = self
}
}

最后,回到第二个 View Controller ,并在 viewWillDisappear 方法中将委托(delegate)的值设置为您想要的值。

func viewWillDisappear(_ animated: Bool) {
self.main_content = contentTextView.text
delegate?.updateMaincontent(main_content: contentTextView.text)
}

关于ios - 使用默认后退按钮时 Unwind 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45136906/

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