iOS基礎(三)

視圖控制器與通知

UIViewController

  • 視圖控制器是數據和視圖之間的橋樑,視圖供職器提供了一個基本的框架來構建應用程序。
  • UIViewController是全部視圖控制器的父類
  • iOS提供了許多內置的視圖控制器,以支持標準的用戶界面部分,好比導航控制器UINavigationController,標籤欄控制器UITabBarController,表視圖控制器UITableViewController

視圖控制器與視圖的關係

  • 視圖控制器是傳統Modle-View-Controller(MVC)設計模式中————控制器對象
  • 視圖控制器提供了許多控制器的基本功能
  • 對於某些功能,基類提供瞭解決方案的一部分,其餘的自定義的功能由視圖控制器的子類去實現,好比:用戶選擇設備,能夠由子類去控制是否旋轉
  • 視圖控制器負責建立和管理一組視圖,它自己提供一個視圖,稱爲該試圖控制器的根視圖,協調管理數據的視圖之間的交互

    • UIScreen對象時連接物理屏幕的標示
    • UIWindow爲UIScreen對象提供畫布
    • 一組UIView對象就能夠顯示內容
    • 每一個視圖控制器管理和控制一系列的視圖
    • 永遠不要把UIView添加到UIWindow上,二是添加一個UIViewController

視圖控制器(UIViewController)的建立

```Objective-C
// 代碼建立
UIViewController *mainViewController = [[UIViewCtroller alloc] init];
mainViewController.view.backgroundColor = [UIColor redColor];
self.window.rootViewController = mainViewCtroller;
// nib建立
RootViewController *rootViewController = [[RootViewController alloc] initWithNibName:@"view" bundle:nil];
self.window.rootViewController = rootViewController;git


##UIViewController生命週期 ![](https://github.com/zt1991616/blog/raw/master/Image/Load.jpg) ```Objective-c -(void)loadView { // 調用父類來建立view // 從nib、storybord加載View,不然建立一個empty view // 建立一個自定義的視圖,覆蓋便可 } -(void)view

加載過程

  • 首先去訪問view屬性
  • 若是存在view,判斷之間加載。不然,則UIViewCtroller調用loadView方法

    • loadView方法執行以下操做

      • 若是覆蓋了該方法,則必須建立View給UIViewCtroller的View屬性
      • 若是沒有覆蓋該方法,UIViewControlller會調用父類的方法

各個方法執行順序

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
        - (void)loadView
        - (void)viewDidLoad
        - (void)viewWillAppear:(BOOL)animated
        - (void)viewDidAppear:(BOOL)animated
        - (void)viewWillDisappear:(BOOL)animated
        - (void)viewDidDisappear:(BOOL)animated

視圖卸載


- iOS6以前使用viewDidUnload:方法來釋放對象的引用
- iOS6以後使用didRecevelMemoryWarning內存緊張的時候調用
1. viewWillDisappear 視圖將被從屏幕上移除以前執行
2. viewDidDisappear 視圖已經被從屏幕上移除,用戶看不到這個視圖
3. dealloc 視圖被銷燬,此處須要對你在init和viewDidLoad中建立的對象進行釋放github

試圖控制器的響應鏈

  • 事件傳遞流程
    當前觸發的事件->視圖控制器的跟視圖->視圖控制器->窗口->UIApplication對象->不處理

模態視圖

  • 模態視圖不是特定的某個類,而是經過視圖控制器的pressntModalViewController:方法彈出的視圖稱爲模態視圖
  • 模態視圖出現的場景通常是臨時彈出的窗口,譬如:登入窗口
  • 模態視圖彈出時經過modalTransitionStyle屬性設置不一樣的動畫效果
  • 調用dismissModalViewControllerAnimated:方法關閉窗口

```Objective-c
-(void)viewDidLoad
{
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundeRect];
button.frame = CGRectMake(320/2 - 140/2,80,140,40);
[button setTitle:@"Present" forState:UIControlStateNormal];
[button addTarget:self action:@selector[presentModalVc] forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
-(void)presentModalVC
{
ModalViewController *modalVc = [[ModalViewController alloc] init];
// 動畫效果
modalVc.modalTranstionSyle = UIModalTransitionStylePatialCurl;
if([[UIDevice currentDevice].systemVersion floatValue] < 6.0){
[self.presentModalViewController:modalVc animated:YES];
}else{
[self.presentModalViewController:modalVc animated:YES completion:^{
NSLog(@"call back");
}];
[modalVC relese];
}
}設計模式


```Objective-c //ModalViewController.m -(void)dismiss { // 將模態視圖關閉 [self dismissViewControllerAnimated:YES completion:^{ NSLog(@"dismiss")]; }]; }

支持多個方向


- iOS設備中的加速計能夠肯定設備的當前方向。默認狀況下,一個應用支持縱向和橫向。當設備方向改變時,系統會發送UIDiviceOrientationDidChangeNotfication通知,默認狀況下UIKit框架監聽這個通知,並自定義更新這個方向。app

```Objective-c
-(void)viewDidLoad
{
// ...
// 採用通知獲取屏幕方向切換
[[UINotificationCenter defaultCenter] addObserver:self selector:@selector(deviceOrientation:)name:UIDeviceOrientationDidChangeNotifitation object:nil];
}
-(void)deviceOrientation:(NSNotification *)notification
{
UIDevice *device = (UIDevice *)[notification object];
NSLog(@"device:%d",device.orientation);
}
-(BOOL)shoudlAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
{
return NO; //NO爲不支持
// return (toInterfaceOrientation != UIterfaceOrientationLandscapeLeft); // 不支持一個方向
}框架

pragma mark - Orientation iOS 3.0_5.0

-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrienttation)toInterfaceOrientation
{
return NO;
}ide

pragma mark - Orientation iOS 6.0

-(BOOL)shouldAutorotate
{
return YES;
}
-(void)willAnimateRotationToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration
{
NSLog(@"duration:%f",duration);
UIView* button = [self.view viewWithTag:101];
if(toInterfaceOrientation == UIInterfaceOrientationPortrait){
button.frame = CGRectMake(320/2-140/2,80,140,40);
}else{
button.frame = CGRectMake(480/2-140/2,80,140,40);
}
}
```動畫

相關文章
相關標籤/搜索