1. What is the initWithFrame method?
The initWithFrame method is used to initialize and return a new View object, based on the specified cgrect (size). Of course, other UI objects, there are initwithframe methods, but we use UIView as an example to figure out the initWithFrame method.
2. When do I use the initWithFrame method?
Simply put, we use the initWithFrame method to programmatically declare that the UIView object is created. Here, we have to figure out two ways to initialize the UIView.
- Use the Interface Builder method. in this way, the nib file is used. Usually we say "drag the controls" in the way. In actual programming, we created the UIView object in interface Builder mode. (That is, by dragging the control), the initWithFrame method method is not called. Because the nib file already knows how to initialize the view. (Because, when we drag the view, we define properties such as length, width, background, etc.). At this point, the Initwithcoder method is called, and we can use the Initwithcoder method to redefine the properties we have set in the nib. That's why you use Initwithcoder: Because Bidviewcontroller.xib's view is a bidquartzfunview type, not a uiview type, it's actually loading an object instance from the nib.
- use programmatic methods. Is that we declare a subclass of UIView and do the "manual" Way of writing code. In practical programming, we use a programmatic approach to create a uiview or to create a uiview subclass. At this point, the initWithFrame method is called to instantiate the UIView. It is particularly important to note that if you overload the initWithFrame method in a subclass, you must first call the initWithFrame method of the parent class. Initializes the custom UIView subclass. Like what:
-(ID) initWithFrame: (CGRect) frame{
self = [Super initwithframe:frame];//First Call the initWithFrame method of the parent class
if (self) {
You can then customize the initialization of this class (the UIView subclass).
_scrollview = [[Uiscrollview alloc] initWithFrame:self.bounds];
[_scrollview setframe:cgrectmake (0, 0, 320, 480)];
_scrollview.contentsize = Cgsizemake (320*3, 480);
[Self addsubview:_scrollview];
}
return self;
}
When we write a program that does not use the nib file (XIB), control the view content with code, you need to call initWithFrame to initialize
-(ID) initWithFrame: (CGRect) frame
{
if (self =[superinitwithframe:frame]) {
Initialize Code
}
return self;
}
Use Initwithcoder to initialize these instance objects when loading the nib file for the view and loading the object instance from the NIB
-(ID) Initwithcoder: (nscoder*) coder
{
if (self =[superinitwithcoder:coder]) {
Initialize Code
}
return self;
}
1.initWithCoder: For. Xib, when you embed a view object into Xib, the method is called by default when the view is loaded, for example: If you create a view from the nib, you will call Initwithcoder, which is called by the system and cannot be called. 2.initWithFrame: Manual encoding of Non. Xib, the method is called by default when the view is loaded. is called by itself to initialize the object's
The difference between Initwithcoder and initWithFrame