Ios 未触发自定义序列的展开
问题 我的问题是,即使单击了“后退”按钮,也不会触发展开过程 我的代码 我有一个类1stViewController,它使用到2ndViewController的自定义序列。自定义序列如下所示:Ios 未触发自定义序列的展开,ios,swift,segue,uistoryboardsegue,unwind-segue,Ios,Swift,Segue,Uistoryboardsegue,Unwind Segue,问题 我的问题是,即使单击了“后退”按钮,也不会触发展开过程 我的代码 我有一个类1stViewController,它使用到2ndViewController的自定义序列。自定义序列如下所示: override func perform() { let sourceVC = self.source as! 1stViewController let destinationVC = self.destination as! 2ndViewController source
override func perform() {
let sourceVC = self.source as! 1stViewController
let destinationVC = self.destination as! 2ndViewController
sourceVC.addChildViewController(destinationVC)
sourceVC.view.addSubview(destinationVC.view)
}
在我的2ndViewController中,我添加了一个按钮,该按钮可触发一个退绕序列返回到1SDVIEWCONTROLLER。退绕段通过第二个DVIEWController中的退绕段触发出口的标识符连接。因此,我创建了一个IBAction,该IBAction与展开序列出口相连:
@IBAction func unwindToFeed(segue: UIStoryboardSegue) {}
我已将展开序列的标识符设置为按钮动作功能中使用的标识符
func handleActionBackButton(){
print("here")
self.performSegue(withIdentifier: "unwindId", sender: self)
}
单击按钮时会打印“Here”,但似乎performsgue不会
我猜我还需要一个自定义unwindSegue类,所以我创建了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)
}
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 {
}
关于我必须做些什么才能触发退绕过程,有什么线索吗?我得到了你的例子,就是这样工作的。ViewController是第一个视图控制器;ViewController2是第二个视图控制器:
override func segueForUnwinding(to toViewController: UIViewController, from fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue? {
return unwindSegueEventToFeed(identifier: identifier, source: fromViewController, destination: toViewController)
}
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
位于(并且必须位于)第一视图控制器中。将其放在第二视图控制器中是没有意义的,因为第二视图控制器是展开的源,而不是目的地
现在,我想就这个问题和我的答案发表一些评论:
- 这是一个iOS 10示例<在iOS 10中永远不会调用code>segueForUnwinding;它在iOS 8之后被放弃,随后被弃用
- iOS 9/10版本的
是segueforunwind
。我确实尝试过实现它,但我发现它是在ViewController2中调用的,而不是在ViewController中调用的。我认为这是iOS 10中的一个bug,我正在向苹果公司报告unwind(用于:towardsViewController:)
- 我得到了你这样的例子。ViewController是第一个视图控制器;ViewController2是第二个视图控制器:
override func segueForUnwinding(to toViewController: UIViewController, from fromViewController: UIViewController, identifier: String?) -> UIStoryboardSegue? {
return unwindSegueEventToFeed(identifier: identifier, source: fromViewController, destination: toViewController)
}
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
位于(并且必须位于)第一视图控制器中。将其放在第二视图控制器中是没有意义的,因为第二视图控制器是展开的源,而不是目的地
现在,我想就这个问题和我的答案发表一些评论:
- 这是一个iOS 10示例<在iOS 10中永远不会调用code>segueForUnwinding;它在iOS 8之后被放弃,随后被弃用
- iOS 9/10版本的
是segueforunwind
。我确实尝试过实现它,但我发现它是在ViewController2中调用的,而不是在ViewController中调用的。我认为这是iOS 10中的一个bug,我正在向苹果公司报告unwind(用于:towardsViewController:)
unwindToFeed
在1stViewController中?unwindToFeed在2NDVIEWController中,位置错误。展开方法位于要展开的视图控制器中,但展开进纸不应该位于要取消的视图中吗?2ndViewController是用户可以看到的按钮所在位置的视图。您可以从任意数量的VC退绕到一个VC,因此退绕操作方法为您提供了处理退绕的一致位置。源代码中不需要展开方法,因为可以使用简单的按钮操作处理程序,甚至可以直接从UI元素触发展开,而无需代码。苹果有一个很好的技术说明,关于使用展开段,必须确认,展开进纸
在1stViewController中?展开进纸在2NDVIEWCONTROLLER中,这是错误的位置。展开方法位于要展开的视图控制器中,但展开进纸不应该位于要取消的视图中吗?2ndViewController是用户可以看到的按钮所在位置的视图。您可以从任意数量的VC退绕到一个VC,因此退绕操作方法为您提供了处理退绕的一致位置。源代码中不需要展开方法,因为可以使用简单的按钮操作处理程序,甚至可以直接从UI元素触发展开,而无需代码。苹果在使用unwind Segues方面有很好的技术说明,但我的示例在iOS 9中根本不起作用。总的来说,在自定义父视图控制器中使用“展开分段”的整个概念看起来仍然很不成熟,多年来一直如此,我建议完全避免使用它。但我的示例在iOS 9中根本不起作用。总的来说,在自定义父视图控制器中使用“展开分段”的整个概念看起来仍然很不完善,多年来一直如此,我建议完全避免使用它。