LoadView: functions in UIView iOS

I do not understand the mechanism of the loadView: function (this function is in a UIView).

I created a project as shown below:

  • First, I created a project based on the iPhone window.
  • Then I created a subclass of UIView
  • Then I created a subclass of UIViewController without xib.
  • Finally, in the loadView: function loadView: class I created in the third step, I designate the UIView object (in class I created in the second step) as a variable of the form of the UIViewController object (in the third step).

If I omitted the last step and placed the NSLog(@"test LoadView"); statement NSLog(@"test LoadView"); to the loadView function: when the project is running, the NSLog(@"test LoadView"); instruction NSLog(@"test LoadView"); called continuously, as a result, overflow occurs.

Please explain me! Thanks!

+7
source share
1 answer

loadView: only called when a property of the form nil . Use this when creating views programmatically. default: create a UIView object without peeping. 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 are connected to the default memory management behavior. If memory is low, the view controller may receive a didReceiveMemoryWarning message. The default implementation checks if the view is being used. If its view is not in the view hierarchy, and the view controller implements the loadView: method, its view will be released. Later, when a view is needed, the loadView: method is loadView: again to create the view.

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

Link -

Hope this helps.

+15
source

All Articles