loadView Problem..

 

loadView Problem..

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?
sujithkrishnan is offline   Reply With Quote

Old Aug 28, 2008, 04:14 AM   #2
Contributor
 
kainjow's Avatar
 
Join Date: Jun 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.
kainjow is offline   Reply With Quote
Old Aug 28, 2008, 05:11 AM   #3
Thread Starter
macrumors regular
 
Join Date: May 2008
Location: Bangalore
Send a message via Yahoo to sujithkrishnan
Quote:
Originally Posted by kainjow View Post
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....
sujithkrishnan is offline   Reply With Quote
Old Aug 28, 2008, 05:17 AM   #4
macrumors Demi-God
 
robbieduncan's Avatar
 
Join Date: Jul 2002
Location: London
Quote:
Originally Posted by sujithkrishnan View Post
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.
__________________
My App Store Apps: DLR Times
robbieduncan is offline   Reply With Quote
Old Aug 28, 2008, 05:56 AM   #5
macrumors regular
 
Join Date: Nov 2007
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.
Luke Redpath is offline   Reply With Quote
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值