使用默认后退按钮时 Unwind 方法不起作用
Unwind method doesn't work when using default backward button
我有如下三个视图控制器
我在viewcontroller1中写了unwind方法,并尝试在viewcontroller2和viewcontroller3展开到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
}
并且已经为控制器 2 和 3 设置了退出展开转场。
但为什么 unwindToViewController 方法从未被正确调用?我觉得应该是点击系统自动生成的按钮时调用的。
我通过使用委托而不是展开来解决这个问题。 Delegate pattenr 是解决这个问题的一种更明确的方式。
通过创建一个名为 myDelegate
的协议
protocol myDelegate {
func updateMaincontent(main_content : String)
func updateSupplement(supplement: String)
}
并在第二个视图控制器中创建一个委托实例
var delegate: myDelegate?
然后在第一个视图控制器中,使 class 扩展 myDelegate
并在 func prepare(for segue: UIStoryboardSegue, sender: Any?
) 方法中将第二个视图控制器的委托设置为 self
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
}
}
最后,回到第二个视图控制器,并在 viewWillDisappear 方法中将委托的值设置为您想要的值。
func viewWillDisappear(_ animated: Bool) {
self.main_content = contentTextView.text
delegate?.updateMaincontent(main_content: contentTextView.text)
}
我有如下三个视图控制器
我在viewcontroller1中写了unwind方法,并尝试在viewcontroller2和viewcontroller3展开到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
}
并且已经为控制器 2 和 3 设置了退出展开转场。
但为什么 unwindToViewController 方法从未被正确调用?我觉得应该是点击系统自动生成的按钮时调用的。
我通过使用委托而不是展开来解决这个问题。 Delegate pattenr 是解决这个问题的一种更明确的方式。 通过创建一个名为 myDelegate
的协议protocol myDelegate {
func updateMaincontent(main_content : String)
func updateSupplement(supplement: String)
}
并在第二个视图控制器中创建一个委托实例
var delegate: myDelegate?
然后在第一个视图控制器中,使 class 扩展 myDelegate
并在 func prepare(for segue: UIStoryboardSegue, sender: Any?
) 方法中将第二个视图控制器的委托设置为 self
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
}
}
最后,回到第二个视图控制器,并在 viewWillDisappear 方法中将委托的值设置为您想要的值。
func viewWillDisappear(_ animated: Bool) {
self.main_content = contentTextView.text
delegate?.updateMaincontent(main_content: contentTextView.text)
}