Correct way of removing (deleting) a view/viewController from the stack after segue in Swift

本秂侑毒 提交于 2019-12-04 14:33:02

If your goal is to always keep one VC, and since you already have a manual segue, what you can do is, after presenting the next VC you can pop the existing VC from the root and assign the destination VC as your root VC like so

class ManualSegue: UIStoryboardSegue {

  override func perform() {
    sourceViewController.presentViewController(destinationViewController, animated: true) {
      self.sourceViewController.navigationController?.popToRootViewControllerAnimated(false)
      UIApplication.sharedApplication().delegate?.window??.rootViewController = self.destinationViewController
    }
  }
}

When you do normal segues, you are piling up new view controllers on top of the existing ones without dismissing them. That's why your memory usage keeps going up.

You need to provide more information before we can give you specific guidance. Are you doing modal segues or pushing onto a navigation stack?

If you're pushing, you want to pop to the view controller you came from. Think in terms of a stack of plates. When you push, you add plates to the stack. When you pop, you take plates off and reveal the plates (view controllers) underneath. Do a search in the Xcode help system on "PopTo" to find the methods that let you either pop back to a specific view controller or back to the root view controller of your navigation controller.

Modal view controllers stack on top of each other in a similar fashion, but without the origination of a navigation controller. If you've pushed a series of one or more modals and you send the dismissViewControllerAnimated:completion: method to the view controller you want get back to it will dismiss the modals that are on top of it.

You can also look at using unwind segues. Unwind segues let you go back where you came. Do a search in the Xcode help system on "Using unwind segues" for more information. Most of the tutorials on unwind segues are written in Objective-C but you should be able to find some in Swift if you look. Explaining unwind segues in enough detail to be useful is more than I can cover in this answer.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!