loadView Problem..

Discussion in 'iOS Programming' started by sujithkrishnan, Aug 28, 2008.

  1. sujithkrishnan macrumors 6502

    May 9, 2008
    HI all.

    In my iPhone App, am adding a subView(button , image view etc) to the view of a viewController in its loadView method...

    But the program is going in a infinite-loop...

    Problem is that it will call loadView for the first time, and when it reaches the line where i add button

    [self.view addSubView:button];

    it is again calling the method loadView.

    So my question is what all i can do in
    loadView ???

    Is it okay to add all those subViews "init" and leave "loadView" as blank ??

    If i want to change add/remove subviews dynamically, i cant do all stuffs in "init" right?

    Adding to above questions.. i found in most of sample apps, control is not coming to "dealloc" in its classes (espclly viewControllers classes)...

    So what happen to the Occupied views and subviews in memory?
  2. kainjow Moderator emeritus


    Jun 15, 2000
    loadView is called because self.view doesn't exist. When you call self.view and the view doesn't exist yet, the view controller automatically calls loadView to set it up. But since you're calling self.view inside loadView, it keeps calling loadView recursively forever. So don't use self.view, use the view you're creating. If you're loading the view from a nib, don't override loadView and instead override viewDidLoad.

    About your dealloc question, the OS won't always call dealloc if you're quitting the app because when you quit the OS frees up all the memory associated with your app automatically, so it's a waste of time to manually call dealloc.
  3. sujithkrishnan thread starter macrumors 6502

    May 9, 2008

    I am not talking about manually calling "dealloc"...

    As my app is having many levels of view controller ( say 6..7 levels)..
    I am trying to deallocate the present view controller, when i pop That view controller...

    All that stuffs are doing perfectly....

    And moreover... how come OS will re-collect all the memory, as there no GC in iPhone..
    (Not asking the OS functionality ...: )
    what i thought was i want to deallocate each and every stuffs i used.. before quitting the app...

    OS will do it even in iPhone ,, right?

    Also i notyice that my loadView is not calling if i am attaching the ViewController to a NavigationController, then that navigationController to a TabbArController...

    Thanks...a lot for the self.view tip....
  4. robbieduncan Moderator emeritus


    Jul 24, 2002
    Garbage Collection and the OS recovering all memory from a process when it ends are totally and completely different. GC runs during the process and automatically frees objects not referenced. This is not available on the iPhone. The OS recovering all memory when a process ends is just that: when the process ends all of the resources it had are returned to the OS.
  5. Luke Redpath macrumors 6502a

    Nov 9, 2007
    Colchester, UK
    As somebody mentioned above, the job of loadView is to create the object you are trying to reference when you call self.view, but it doesn't exist when you call self.view hence the infinite loop.

    I suspect you are probably missing a call to [super loadView] at the top of your overridden loadView method.
  6. sradforth macrumors newbie

    Aug 24, 2010
    This is just spot on information! I encountered exactly this too and explains what I have been pulling my hair out about as it worked fine when I had a NIB but went crazy when only procedural setup code was used. To be fair it is documented now I delve into the header file but knowing this is what is happening to know to look for it is the real trick :)
  7. Jeromel macrumors newbie

    Aug 6, 2011
    Thank you Luke, today you save my life !! :) ... after two days of back and forth ...
  8. PhoneyDeveloper macrumors 68040


    Sep 2, 2008
    If you call [super loadView] at the start of your loadView override UIViewController will create a UIView that fills the screen and assign it to self.view. You can use that view or instead you can create your own UIView or subclass of UIView to be the container view and assign that to self.view. Either way you need to have self.view assigned before you access it in your loadView override.

Share This Page