未触发自定义 segue 的展开
Unwind of custom segue not triggered
问题
我的问题是即使单击后退按钮也不会触发展开过程。
我的代码
我有一个 class 1stViewController,它使用自定义 segue 到 2ndViewController。自定义转场如下:
override func perform() {
let sourceVC = self.source as! 1stViewController
let destinationVC = self.destination as! 2ndViewController
sourceVC.addChildViewController(destinationVC)
sourceVC.view.addSubview(destinationVC.view)
}
在我的 2ndViewController 中,我添加了一个按钮,该按钮触发返回到 1stViewController 的展开转场。 unwind segue 通过 2ndViewController 中 unwind segue 触发出口的标识符连接。因此,我创建了一个 IBAction,unwind segue 出口连接到:
@IBAction func unwindToFeed(segue: UIStoryboardSegue) {}
我已经将 unwind segue 的标识符设置为等于按钮的操作函数中使用的标识符
func handleActionBackButton(){
print("here")
self.performSegue(withIdentifier: "unwindId", sender: self)
}
单击按钮时会打印 "Here" ,但似乎不会打印 performSegue 。
我猜想我也需要自定义 unwindSegue class,所以我创建了 unwindSegueEventToFeed:
class unwindSegueEventToFeed: UIStoryboardSegue {
override func perform() {
let sourceVC = self.source as! 2ndViewController
let destinationVC = self.destination as! 1stViewController
let sourceView = sourceVC.view as UIView!
let destView = destinationVC.view as UIView!
let window = UIApplication.shared.keyWindow
window?.insertSubview(destView!, aboveSubview: sourceView!)
sourceVC.dismiss(animated: false, completion: nil)
}
}
...在 2ndViewController 中调用:
override func segueForUnwinding(to toViewController: UIViewController, from fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue? {
return unwindSegueEventToFeed(identifier: identifier, source: fromViewController, destination: toViewController)
}
关于触发平仓过程我必须做些什么的任何线索?
我让你的例子像这样工作。 ViewController 是第一个视图控制器; ViewController2 是第二个视图控制器:
class ViewController: UIViewController {
@IBAction func unwindToFeed(_ segue: UIStoryboardSegue) {
let child = self.childViewControllers[0]
child.willMove(toParentViewController: nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}
}
class MySegue: UIStoryboardSegue {
override func perform() {
let sourceVC = self.source as! ViewController
let destinationVC = self.destination as! ViewController2
sourceVC.addChildViewController(destinationVC)
sourceVC.view.addSubview(destinationVC.view)
destinationVC.view.frame = sourceVC.view.bounds
destinationVC.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
destinationVC.didMove(toParentViewController: sourceVC)
}
}
class ViewController2 : UIViewController {
}
请注意 unwindToFeed
是(并且必须是)在 first 视图控制器中。将它放在第二个视图控制器中是没有意义的,因为第二个视图控制器是 source,而不是展开的 destination。
现在我想对问题和我的回答做一些评论:
这是一个 iOS 10 的例子。 segueForUnwinding
将 永远不会 在 iOS 10 中被调用;它在 iOS 8 之后被废弃,随后被弃用。
segueForUnwinding
的 iOS 9/10 等价于 unwind(for:towardsViewController:)
。我确实尝试实现它,但我发现它是在 ViewController2 中调用的,而不是在 ViewController 中调用的。我认为这是 iOS 10 中的错误,并已将其报告给 Apple。
问题
我的问题是即使单击后退按钮也不会触发展开过程。
我的代码
我有一个 class 1stViewController,它使用自定义 segue 到 2ndViewController。自定义转场如下:
override func perform() {
let sourceVC = self.source as! 1stViewController
let destinationVC = self.destination as! 2ndViewController
sourceVC.addChildViewController(destinationVC)
sourceVC.view.addSubview(destinationVC.view)
}
在我的 2ndViewController 中,我添加了一个按钮,该按钮触发返回到 1stViewController 的展开转场。 unwind segue 通过 2ndViewController 中 unwind segue 触发出口的标识符连接。因此,我创建了一个 IBAction,unwind segue 出口连接到:
@IBAction func unwindToFeed(segue: UIStoryboardSegue) {}
我已经将 unwind segue 的标识符设置为等于按钮的操作函数中使用的标识符
func handleActionBackButton(){
print("here")
self.performSegue(withIdentifier: "unwindId", sender: self)
}
单击按钮时会打印 "Here" ,但似乎不会打印 performSegue 。
我猜想我也需要自定义 unwindSegue class,所以我创建了 unwindSegueEventToFeed:
class unwindSegueEventToFeed: UIStoryboardSegue {
override func perform() {
let sourceVC = self.source as! 2ndViewController
let destinationVC = self.destination as! 1stViewController
let sourceView = sourceVC.view as UIView!
let destView = destinationVC.view as UIView!
let window = UIApplication.shared.keyWindow
window?.insertSubview(destView!, aboveSubview: sourceView!)
sourceVC.dismiss(animated: false, completion: nil)
}
} ...在 2ndViewController 中调用:
override func segueForUnwinding(to toViewController: UIViewController, from fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue? {
return unwindSegueEventToFeed(identifier: identifier, source: fromViewController, destination: toViewController)
}
关于触发平仓过程我必须做些什么的任何线索?
我让你的例子像这样工作。 ViewController 是第一个视图控制器; ViewController2 是第二个视图控制器:
class ViewController: UIViewController {
@IBAction func unwindToFeed(_ segue: UIStoryboardSegue) {
let child = self.childViewControllers[0]
child.willMove(toParentViewController: nil)
child.view.removeFromSuperview()
child.removeFromParentViewController()
}
}
class MySegue: UIStoryboardSegue {
override func perform() {
let sourceVC = self.source as! ViewController
let destinationVC = self.destination as! ViewController2
sourceVC.addChildViewController(destinationVC)
sourceVC.view.addSubview(destinationVC.view)
destinationVC.view.frame = sourceVC.view.bounds
destinationVC.view.autoresizingMask = [.flexibleHeight, .flexibleWidth]
destinationVC.didMove(toParentViewController: sourceVC)
}
}
class ViewController2 : UIViewController {
}
请注意 unwindToFeed
是(并且必须是)在 first 视图控制器中。将它放在第二个视图控制器中是没有意义的,因为第二个视图控制器是 source,而不是展开的 destination。
现在我想对问题和我的回答做一些评论:
这是一个 iOS 10 的例子。
segueForUnwinding
将 永远不会 在 iOS 10 中被调用;它在 iOS 8 之后被废弃,随后被弃用。segueForUnwinding
的 iOS 9/10 等价于unwind(for:towardsViewController:)
。我确实尝试实现它,但我发现它是在 ViewController2 中调用的,而不是在 ViewController 中调用的。我认为这是 iOS 10 中的错误,并已将其报告给 Apple。