Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

loadView: functions in UIView iOS

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!

like image 215
vietstone Avatar asked Oct 17 '11 02:10

vietstone


People also ask

What is loadView in iOS?

loadView() Creates the view that the controller manages. iOS 2.0+ iPadOS 2.0+ Mac Catalyst 13.0+ tvOS 9.0+

What is UIViewController in iOS?

The UIViewController class defines the shared behavior that's common to all view controllers. You rarely create instances of the UIViewController class directly. Instead, you subclass UIViewController and add the methods and properties needed to manage the view controller's view hierarchy.

What is Load view in Swift?

loadView( ) is a method managed by the viewController. The viewController calls it when its current view is nil. loadView( ) basically takes a view (that you create) and sets it to the viewController's view (superview).


1 Answers

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.

like image 176
Srikar Appalaraju Avatar answered Oct 03 '22 09:10

Srikar Appalaraju