0

I have read Apple's design patterns docs, and a few other guides and there are things I can not understand .

I encounter the problem of passing variables between viewControllers, and I saw the delegate option. Than i have realize that if you go from viewControlA to viewControlB , and you need to update some mutableArray from B to A , you can post a delegate from B and A will get it .

BUT, if A can hear the delegate, that means that A is still alive after I went to B . I was thinking that only when you push between views, the previous is still alive, but when the transition is modal, the previous scene is actually dead .

What is the life cycle of each view controller class ? They are always alive ?

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
Curnelious
  • 1
  • 16
  • 76
  • 150

1 Answers1

2

If you have two UIViewControllers called A and B, and you want to show B modally, A stays in memory. No one says to A to remove (this is true until some other part of the code will remove it).

So, A can respond to B until the latter (B) remains the presentedViewController of A (presentingViewController).

About the delegate, you could just avoid it. Suppose for example that A and B as a property like

@property (nonatomic, strong) NSMutableArray* myArray;

Before presenting B as the modal controller, you can say

B* b = // alloc init
b.myArray = [self myArray];
// present modally B

Now they will touch the same array. When B is dismissed (it will released from memory if you have no references to it), in myArray (within A) you will find the modifications done in B.

Obviously this is just an example. And this is not an advice to not using delegates.

For further references I would just take a look to Presenting View Controllers from Other View Controllers in Apple Doc.

Lorenzo B
  • 33,216
  • 24
  • 116
  • 190
  • thanks very much! so when does A cleared from memory ? if i go from A to B and C ,all the chain is in memory ? and if i go from B to A by dismissing B , so than B is dead ? – Curnelious Jan 11 '14 at 12:37
  • @Curnelious You need to think in term of objects graph. ROOT-->A-->B. If B is dismissed, now you have ROOT-->A. If ROOT dismiss A, you will have only ROOT. P.S. Upvote too if you want. Cheers. – Lorenzo B Jan 11 '14 at 12:41
  • @Curnelious Obviously you need to look at retain cycles. See my answer on this http://stackoverflow.com/questions/11168916/weak-or-strong-for-iboutlet-and-other/11170327#11170327. It covers a lot of aspects. – Lorenzo B Jan 11 '14 at 12:43