gpt4 book ai didi

ios - Unwind Segue 不使用 UIModalPresentationCustom 关闭 View Controller

转载 作者:可可西里 更新时间:2023-11-01 03:34:22 27 4
gpt4 key购买 nike

我正在使用自定义转换呈现模态视图 Controller (通过将其 modelPresentationStyle 设置为 UIModalPresentationCustom,提供转换委托(delegate)和 UIViewControllerAnimatedTransitioning 对象)。

在呈现的 View Controller 中,我有一个连接到按钮的展开转场。 segue 火得很好;呈现 View Controller 中的 IBAction 方法被调用,呈现 View Controller 中的 prepareForSegue 也被调用。但是,呈现的 View Controller 不会被取消,并且不会调用适当的转换委托(delegate)方法 (animationControllerForDismissedController:)。

但是,如果我将呈现的 View Controller 的 modalPresentationStyle 设置为 UIModalPresentationFullScreen(默认值),则 View Controller 会被正确关闭(但这会破坏我的自定义转换) .

我完全不知道在这里做什么。我查看了 Apple 的文档,并没有注意到任何关于在处理自定义转换时必须对 unwind segues 做特殊事情的说法。

我知道我可以在呈现 View Controller 的 IBAction 方法中调用 dismissViewControllerAnimated:completion:,但我宁愿将其用作最后的手段,并让 unwind segue 以它应该的方式工作(或者至少知道为什么它不工作 :))。

任何帮助将不胜感激,

提前致谢

最佳答案

似乎如果您使用 UIModalPresentationCustom 向 Controller 呈现自定义转换管理器,您还需要使用自定义转换管理器来关闭它(我想这是有道理的,因为您可以animator 对象和 UIKit 中的各种奇怪的东西不能确定像往常一样关闭屏幕是否会完全恢复原始状态 - 我只是希望它明确地告诉你......)。

以下是我在我的应用中解决此问题的方法:

  • 覆盖父 View Controller 中的 segueForUnwindingToViewController(您在关闭动画后移动到的 View Controller )并返回您的 UIStoryboardSegue 实例,无论是您'用于原始转换或新的单独类
  • 如果 unwind segue 的目标 View Controller 在导航层次结构中,那么您需要在导航 Controller 中覆盖该方法
  • perform方法中调用dismissViewControllerAnimated
  • 呈现的 View Controller 需要仍然持有对转换委托(delegate)的有效引用,否则您将获得 EXC_BAD_ACCESS(请参阅 DismissViewControllerAnimated EXC_Bad_ACCESS on true)- 因此要么将委托(delegate)保留为该线程中所述的强引用,或者在调用 dismissViewControllerAnimated 之前分配一个新的(有可能将 modelPresentationStyle 更改为例如在关闭之前全屏也可以,但我还没有尝试过)
  • 如果关闭动画需要做任何非标准的事情(幸运的是我没有),覆盖转换管理器对象中的 animationControllerForDismissedController 并返回一个合适的动画师
  • 如果目标 View Controller 在导航层次结构中,那么您还需要在关闭呈现的屏幕之前手动将导航堆栈弹出到目标 Controller (即 target.navigationController!.popToViewController(target, animated: false ))

完整代码示例:

// custom navigation controller 

override func segueForUnwindingToViewController(toViewController: UIViewController,
fromViewController: UIViewController,
identifier: String?) -> UIStoryboardSegue {
return CustomSegue(
identifier: identifier,
source: fromViewController,
destination: toViewController
)
}


// presented VC

var customTransitionManager: UIViewControllerTransitioningDelegate?


// custom segue

override func perform() {
let source = sourceViewController as! UIViewController

if let target = destinationViewController as? PresentedViewController {
let transitionManager = TransitionManager()
target.modalPresentationStyle = .Custom
target.customTransitionManager = transitionManager
target.transitioningDelegate = transitionManager

source.presentViewController(target, animated: true, completion: nil)
} else if let target = destinationViewController as? WelcomeViewController {
target.navigationController!.popToViewController(target, animated: false)
target.dismissViewControllerAnimated(true, completion: nil)
} else {
NSLog("Error: segue executed with unexpected view controllers")
}
}

关于ios - Unwind Segue 不使用 UIModalPresentationCustom 关闭 View Controller,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28719043/

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