mirror of
https://github.com/NohamR/knowledge-kit.git
synced 2026-05-24 20:00:37 +00:00
Add 3 chapter notes and update chapter index
This commit is contained in:
81
Chapter1 - iOS/1.3.md
Normal file
81
Chapter1 - iOS/1.3.md
Normal file
@@ -0,0 +1,81 @@
|
||||
# loadView
|
||||
|
||||
1. Purpose: load the controller's view
|
||||
2. When it's called: it's called the first time the controller's view is used
|
||||
3. Use case: implement this method when you want to provide a custom view for the controller
|
||||
|
||||
Accessing the controller's view is equivalent to calling the controller's view getter:
|
||||
```objective-c
|
||||
-(UIView *)view
|
||||
{
|
||||
if(_view == nil){
|
||||
[self loadView];
|
||||
[self viewDidLoad];
|
||||
}
|
||||
return _view;
|
||||
}
|
||||
```
|
||||
|
||||
# Controller view loading flow
|
||||

|
||||
|
||||
- The controller's `init` method internally calls `initWithNibName`
|
||||
|
||||
`MyViewController *vc = [[MyViewController alloc] init];`
|
||||
|
||||
Notes:
|
||||
|
||||
The system's decision logic assumes: no nibName specified; no custom loadView method; the controller is named ...Controller
|
||||
|
||||
Decision rules:
|
||||
|
||||
- Check whether a nibName was specified; if specified, load that nib
|
||||
- Check whether there's an xib with the same name as the controller but without "Controller" in its name; if present, load it
|
||||
- If the previous step doesn't find one, check whether an xib with the same name as the controller class exists; if present, load it
|
||||
- If no xib describes the controller's view, do not load any xib
|
||||
|
||||
## How MyViewController loads its view
|
||||
|
||||
- Check whether an xibName was specified; if so, load the specified xib
|
||||
- Check whether there's an xib with the same name as the controller class but without "Controller" in the name
|
||||
- Check whether there's an xib with the same name as the controller class; if present, load it
|
||||
- Otherwise, create an empty view directly
|
||||
|
||||
Example
|
||||
|
||||
```objective-c
|
||||
// in AppDelegate
|
||||
ViewController *vc = [[ViewController alloc] init];
|
||||
vc.view.backgroundColkor = [UIColor redColor];
|
||||
self.window.rootViewController = vc;
|
||||
[pself.window makeKeyAndVisable];
|
||||
|
||||
// ViewController
|
||||
-(UIView *)view{
|
||||
if(!_view){
|
||||
[self loadView];
|
||||
[self viewDidLoad];
|
||||
}
|
||||
}
|
||||
|
||||
-(void)loadView{
|
||||
UIView*view = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
|
||||
view.backgroundColor = [UIColor greenColor];
|
||||
self.view = view;
|
||||
}
|
||||
|
||||
-(void)viewDidload{
|
||||
[super viewDidload];
|
||||
self.view.backgroundColor = [UIColor brownColor];
|
||||
}
|
||||
```
|
||||
|
||||
### What color will the interface be at this point?
|
||||
|
||||
Many people might answer "green." Actually, the answer is red.
|
||||
|
||||
Why? In AppDelegate, `vc.view.backgroundColor` calls vc's view getter. Inside the getter it checks if `_view` exists; if not, it creates a new UIView by calling `[self loadView]`. After creating the view it calls `viewDidLoad`; if the view already exists it returns it directly. So the sequence is: first green, then brown, and finally red.
|
||||
|
||||
#### An official explanation
|
||||
|
||||

|
||||
Reference in New Issue
Block a user