8

In the WWDC 2012 Session titled The Evolution of View Controllers on iOS - (void)willMoveToParentViewController:(UIViewController *)parent and - (void)didMoveToParentViewController:(UIViewController *)parent have been mentioned to be used in custom container implementation for view transitions.

If I'm not mistaken, it seems to require to call these methods EXPLICITLY, either before - (void)removeFromSuperview or after - (void)addSubview:(UIView *)view

However, when I logged them inside sub class implementations, it shows that they have been called IMPLICITLY. They have been called at the right places automatically.

So, I'm a little confused if these methods ARE REALLY NEEDED to be called explicitly, like instructed in the WWDC Session, or it's ok not to follow it and clear out unnecessary lines of codes.

petershine
  • 3,090
  • 22
  • 48
  • Sorta-ish duplicate of [How does View Controller Containment work in iOS 5?](http://stackoverflow.com/questions/8379759/how-does-view-controller-containment-work-in-ios-5) – CodaFi Nov 29 '12 at 02:25

1 Answers1

13

Explained in my book:

http://www.apeth.com/iOSBook/ch19.html#_container_view_controllers

Basically one of the two is indeed implicitly called but the other you must call explicitly. It's maddening; I have to look up the rules every time.

matt
  • 447,615
  • 74
  • 748
  • 977
  • The confusion I had was caused by seeing redundant logs of `didMoveToParentViewController` when `removeFromSuperview` and `removeFromParentViewController` was called as a pair, while the view was animating. Thank you for clarification, and I did able to confirm proper usage – petershine Dec 03 '12 at 12:02
  • The book the poster mentions is free, online, and useful. I edited the answer to make that clear but my edit was rejected. – Benjohn Apr 03 '14 at 08:19
  • hey @matt! i've got one for you: the only thing is then, viewWillAppear (and so on) **are called twice**. It seems really - messy. For example, I am merely putting a fixed VC inside a box on the screen. so addChildVC, then addSubview, then didMoveToParent. And for that you get viewWillAppear. BUT apple seem to sneakily avoid this problem if you use storyboard container view. (I have to choose one of many VCs, so, am doing it manually.) Sucks! thanks for this post – Fattie Aug 31 '14 at 14:34
  • @JoeBlow If you're getting `viewWillAppear` twice, you're doing something wrong. I've got examples that show how to do it, logging the heck out of everything to prove that the right messages arrive at the right time: for example, https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p316containerController/ch19p631containerController/ViewController.swift along with https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch06p316containerController/ch19p631containerController/FirstViewController.m which logs... – matt Aug 31 '14 at 15:22
  • Hmm - I really appreciate the comment @Matt, thanks. I pared down the issue here ... http://stackoverflow.com/questions/25593285/ ... note that interestingly I'm making a BRAND NEW vc (not a case people usually test, since it's trivial and forgettable and everyone uses container views now) ... so .. I don't know. Again thanks! – Fattie Aug 31 '14 at 15:27
  • shit :-) the problem was just that the "overall" view up a few levels was sliding on to the screen, so you get the usual two calls to viewWillAppear. in short sorry to bother you! :) awesome reference book you've cooked up there, thanks @Matt – Fattie Aug 31 '14 at 15:31
  • @JoeBlow What you did (making sure everything was okay by logging) was very good. If only everyone always did that...! – matt Aug 31 '14 at 15:36
  • i'm a logging maniac dude :) TBC of course normally i'd just use a storyboard container there .. http://stackoverflow.com/a/23403979/294884 BUT as you know, as yet there's no really good way to have a selection of more than one container, on the one container ... http://stackoverflow.com/questions/25550678 IMO it's really best to simply have a view to mark the area, and simply, instantiate and load one of the choices. AGAIN THANKS HAVE A GOOD "WEEKEND" what's left @matt – Fattie Aug 31 '14 at 16:06