0

This is our scenario,we have VC A,B and C. As well, we have VC X.

I need to use & present VC X within A,B and C.

I don't want to re-create an instance every time i'm on each VC. So basically i was thinking on creating global instance/property at the App Delegate and initialize it there,and just present VC X when needed.

 vcX = self.storyboard?.instantiateViewControllerWithIdentifier("IAP") as! newIAPClass

Does this could cause unexpected behavior?

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Roi Mulia
  • 5,626
  • 11
  • 54
  • 105

1 Answers1

1

Here are some thoughts on reusing a single copy of a viewController X:

  1. Thread safety should have nothing to do with it as all user interface interactions need to be performed on the main thread.

  2. Each instance of a viewController can only have at most one parent at a time. So as long as you only use X once in your view hierarchy, you should be OK. For example A presents B which presents X, and then X returns to B which returns to A. Then A presents C which presents X. This would work fine since X only has one parent at any given time.

  3. Doing this would mean that X would retain all of its settings between presentations, so it would be the responsibility of the presenter to clean it up and properly initialize it before presentation. By keeping a permanent global copy of X around, it will retain all of the data it has between calls. For example, if there is an input text field, that text field will still contain the text from a previous use. The presenter will be responsible for reinitializing any data that viewController X retains and uses. This would include a delegate pointer.


An example of a scenario that would cause problems: A, B, and C are all contained in a UITabBarController. Each one can present X so that the user can change settings. This will fail if A presents X and then the user switches tabs to B and then presents X again because now X will have both A and B as parents, which can't work.


Instead of creating viewController NewIAPClass as a global, consider making it a Singleton. See this answer: Creating a Singleton in Swift

So you'd create NewIAPClass like this:

class NewIAPClass: UIViewController {
    static let sharedInstance = UIStoryboard(name:"Main", bundle:nil).instantiateViewControllerWithIdentifier("IAP") as! NewIAPClass

    // All of the other stuff that makes up NewIAPClass ...
}

Then every time you want to use NewIAPClass, you'd refer to it as NewIAPClass.sharedInstance.

Community
  • 1
  • 1
vacawama
  • 150,663
  • 30
  • 266
  • 294
  • Hey vaca! Thank you for responding. I would like to understand closure number 3.What does "Clean it up" means? assiging nil to the delegation. etc? – Roi Mulia Apr 23 '16 at 11:57
  • As I expected, awesome,thank you! So as long I'm dismissing X, it's should be fine, right? :) btw, does using global property, is fine? (Assuming we are doing as we said) – Roi Mulia Apr 23 '16 at 12:19
  • I actually did tried use of singleton, i know it's the standard, but I couldn't really understand it lol. I will try to find good example of it. Thanks! – Roi Mulia Apr 23 '16 at 12:30
  • Amazing! I just started looking for it, awesome! Thank you! Mind if i ask a question about singleton? Or i should open a new question? I just don't mean to get off subject – Roi Mulia Apr 23 '16 at 12:43
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/110004/discussion-between-roi-mulia-and-vacawama). – Roi Mulia Apr 23 '16 at 12:46
  • Hey @vaca. For the last two days i'm trying to figure this SharedIntance problem. Would you mind take a look? I honestly can't understand what's going on http://stackoverflow.com/questions/36852386/loading-uiviewcontroller-from-instantiateviewcontrollerwithidentifier-nil-outl – Roi Mulia Apr 25 '16 at 23:13