PDA

View Full Version : loadView Problem..




sujithkrishnan
Aug 28, 2008, 04:08 AM
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?



kainjow
Aug 28, 2008, 04:14 AM
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.

sujithkrishnan
Aug 28, 2008, 05:11 AM
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.

kainjow...

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....

robbieduncan
Aug 28, 2008, 05:17 AM
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...


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.

Luke Redpath
Aug 28, 2008, 05:56 AM
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.

sradforth
Sep 23, 2010, 05:06 PM
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.

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 :)

Jeromel
Aug 6, 2011, 09:04 AM
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.

Thank you Luke, today you save my life !! :) ... after two days of back and forth ...

PhoneyDeveloper
Aug 6, 2011, 10:07 AM
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.