3

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?
as diu
  • 1,010
  • 15
  • 31
  • yes why not , but it is not good practice – Shobhakar Tiwari Sep 05 '17 at 19:34
  • What are the implications? Isn't every instantiation calling a separate memory location? Sorry for the questions but I'm curious to know why it wouldn't be good practice. Thank you for responding. – as diu Sep 05 '17 at 19:37
  • Yes, you can push separate instances without incident. Re whether your `MultiPurposeViewController` is a good design or not, (a) it could be, quite possibly, an excellent approach, depending upon the problem you were solving and how you implemented it; (b) it's not relevant to this question here; (c) we don't have enough information here to decide; and (d) this probably isn't even the right forum for that question (e.g. http://codereview.stackexchange.com is good venue for code reviews of functioning code). – Rob Sep 05 '17 at 19:53
  • What's the point of this question? Did you try doing what you asked? Did it work? What issue are you dealing with here? – rmaddy Sep 05 '17 at 19:53
  • @rmaddy this question was to try and see if anybody has encountered such a situation of stacking different instances of the same `UIViewController` onto a `UINavigationController` stack without having any memory issues. Thank you for responding as always. – as diu Sep 05 '17 at 19:57
  • But that's not what your question asked. You simply asked if it can be done. Clearly it can be. You need to update your question with something more specific. – rmaddy Sep 05 '17 at 20:03
  • @rmaddy the question has been updated. Thank you for the feedback! – as diu Sep 05 '17 at 20:06

1 Answers1

4

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.

Shobhakar Tiwari
  • 7,862
  • 4
  • 36
  • 71
  • 2
    Great! So if `MultiPurposeViewController` is designed correctly it can be reused at multiple levels without any memory issues! Thank you. – as diu Sep 05 '17 at 19:56
  • 1
    Yes , Make it correct answer if it solved your issue so that other developer get benefit from your question . Keep coding – Shobhakar Tiwari Sep 05 '17 at 19:59