8

I don't understand the mechanism of loadView: function (this function is in UIView).

I created a project as below:

  • First, I created a iPhone's window-based project.
  • Then, I created a UIView subclass
  • Next, I created a UIViewController subclass, with no xib.
  • Lastly, in the loadView: function of the class I created in the third step, I designate the UIView object (in the class I created in the second step) as the view variable of the UIViewController object (in the third step).

If I omit the last step, and place the statement NSLog(@"test LoadView"); in the loadView: function, then when the project is run, the statement NSLog(@"test LoadView"); is invoked continuously, result in the run is overflow.

Please explain me! Thank you!

Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
vietstone
  • 8,784
  • 16
  • 52
  • 79

1 Answers1

16

loadView: is only invoked when the view property is nil. Use this when creating views programmatically. default: create a UIView object with no subviews. For ex -

- (void)loadView 
{ 
    UIView *view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame]; 
    [view setBackgroundColor:color]; 
    self.view = view; 
    [view release]; 
}

By implementing the loadView: method, you hook into the default memory management behavior. If memory is low, a view controller may receive the didReceiveMemoryWarning message. The default implementation checks to see if the view is in use. If its view is not in the view hierarchy and the view controller implements the loadView: method, its view is released. Later when the view is needed, the loadView: method is invoked again to create the view.

Not sure why you want to use loadView: but you can do just as much in viewDidLoad:

Reference -

  1. Why is this iPhone program not calling -loadView?
  2. loadView

Hope this helps.

Community
  • 1
  • 1
Srikar Appalaraju
  • 71,928
  • 54
  • 216
  • 264
  • Thanks for your help! I have a question: In the case that the view controller use nib file and IBOutlet variables to organize views, if it receive didReceiveMemoryWarning message, is there anyway to release views, then load later when need. Thanks! – vietstone Oct 17 '11 at 03:09
  • Yes ofcourse. so long as you have declared your ui elements as `retain`. See here for more - http://stackoverflow.com/questions/1250518/what-happens-if-i-dont-retain-iboutlet – Srikar Appalaraju Oct 17 '11 at 03:17
  • Dear Srikar, I don't know the way to reload the nib file to the IBOutlet variable after releasing it. And what state is IBOutlet variable at when it is reloaded? Thanks for your help! – vietstone Oct 17 '11 at 04:33
  • Thanks for your help! I don't have enough reputation to vote up. – vietstone Oct 17 '11 at 08:07
  • @camdaochemgio great. Glad i could help to solve your problem. Even if you cannot upvote, you can select this answer as "CORRECT" by checking the "TICK MARK" which will turn to green. This helps the community... – Srikar Appalaraju Oct 17 '11 at 10:42