Search code examples
iosswiftuiviewcontrolleruinavigationcontroller

Is it possible to push 2 separate instances of a UIViewController on a UINavigationContoller stack


I have a generic UIViewController which has multiple uses. It can be instantiated for 6 different ways as it is designed to be very generic. Let us call this multipurpose viewcontroller as MultiPurposeViewController.

Type1VC = MultiPurposeViewController()// initiated differently
self.navigationController.push(Type1VC)

Inside Type1VC:

Type2VC = MultiPurposeViewController()// initiated differently than before
self.navigationController.push(Type2VC)

Is it allowed to do an operation with a navigation controller?

Edit:

  • Will such an operation cause any memory issues?
  • Is such an operation allowed when dealing with UINavigationController stack?
  • Has anybody encountered such a scenario of stacking multiple instances of the same UIViewContoller onto a UINavigationController stack without having any memory leaks?

Solution

  • From Official Apple Doc it is clearly mention :

    To implement a navigation interface, you must decide what data to present at each level of your data hierarchy. For each level, you must provide a content view controller to manage and present the data at that level. If the presentation at multiple levels is the same, you can create multiple instances of the same view controller class and configure each one to manage its own set of data. For example, the Photos app has three distinct presentation types.

    You can read more by following : https://developer.apple.com/library/content/documentation/WindowsViews/Conceptual/ViewControllerCatalog/Chapters/NavigationControllers.html

    Hope this will give you complete idea about your issue. Feel free to comment.