如何在执行 segue 后删除 UIViewController?
How to remove a UIViewController after having performed a segue?
作为练习,我不想使用导航控制器。
我有以下项目:
2 个按钮由 ViewController 中间的每个按钮触发。他们使用以下自定义 class:
class CustomSegue: UIStoryboardSegue {
override func perform() {
weak var firstView = self.source.view as UIView?
weak var secondView = self.destination.view as UIView?
let screenSize = UIScreen.main.bounds.size
secondView?.frame = CGRect(x: screenSize.width, y: 0.0, width: screenSize.width, height: screenSize.height)
UIApplication.shared.keyWindow?.insertSubview(secondView!, aboveSubview: firstView!)
UIView.animate(withDuration: 0.3, animations: { () -> Void in
firstView!.frame = firstView!.frame.offsetBy(dx: -1 * screenSize.width, dy: 0.0)
secondView!.frame = secondView!.frame.offsetBy(dx: -1 * screenSize.width, dy: 0.0)
}) { (_) -> Void in
self.source.present(self.destination, animated: false, completion: nil)
}
}
}
这是非常错误的,因为它在每次执行 segue 时都会创建一个新的 ViewController 对象。由于 UIApplication.shared.keyWindow.subviews
充满了 UIViews...
内存使用量一直在上升,从不下降
我正在处理的项目有几个 ViewController 可以随机调用任何其他项目。由于这个原因我没有成功使用 UIViewController.dismiss(animated:completion:)
因为它系统地使它回到以前的 ViewController.
如何才能在执行 segue 后明确删除之前的 ViewController?
根据评论,您不需要传统的 "Navigation Controller" 功能 - 主要是,您不需要 "Back" 按钮。
因此,一种选择是使用子视图控制器。
您的 "main" 视图控制器将只有一个 "container" 视图。在启动时,您将第一个 VC 作为子 VC 加载,并将其视图添加为容器的子视图。当您想 "navigate" 到任何其他 VC 时,将 VC 作为子 VC 加载,用新的子 VC 替换容器中的当前视图的视图,并卸载当前的 Child VC.
作为练习,我不想使用导航控制器。
我有以下项目:
2 个按钮由 ViewController 中间的每个按钮触发。他们使用以下自定义 class:
class CustomSegue: UIStoryboardSegue {
override func perform() {
weak var firstView = self.source.view as UIView?
weak var secondView = self.destination.view as UIView?
let screenSize = UIScreen.main.bounds.size
secondView?.frame = CGRect(x: screenSize.width, y: 0.0, width: screenSize.width, height: screenSize.height)
UIApplication.shared.keyWindow?.insertSubview(secondView!, aboveSubview: firstView!)
UIView.animate(withDuration: 0.3, animations: { () -> Void in
firstView!.frame = firstView!.frame.offsetBy(dx: -1 * screenSize.width, dy: 0.0)
secondView!.frame = secondView!.frame.offsetBy(dx: -1 * screenSize.width, dy: 0.0)
}) { (_) -> Void in
self.source.present(self.destination, animated: false, completion: nil)
}
}
}
这是非常错误的,因为它在每次执行 segue 时都会创建一个新的 ViewController 对象。由于 UIApplication.shared.keyWindow.subviews
充满了 UIViews...
我正在处理的项目有几个 ViewController 可以随机调用任何其他项目。由于这个原因我没有成功使用 UIViewController.dismiss(animated:completion:)
因为它系统地使它回到以前的 ViewController.
如何才能在执行 segue 后明确删除之前的 ViewController?
根据评论,您不需要传统的 "Navigation Controller" 功能 - 主要是,您不需要 "Back" 按钮。
因此,一种选择是使用子视图控制器。
您的 "main" 视图控制器将只有一个 "container" 视图。在启动时,您将第一个 VC 作为子 VC 加载,并将其视图添加为容器的子视图。当您想 "navigate" 到任何其他 VC 时,将 VC 作为子 VC 加载,用新的子 VC 替换容器中的当前视图的视图,并卸载当前的 Child VC.