iOS開發-UI (四)UIViewController,iosuiviewcontroller

來源:互聯網
上載者:User

iOS開發-UI (四)UIViewController,iosuiviewcontroller

這篇知識很重要,最好全掌握:

知識點:

1.UIViewController基本認識

2.UIViewController之間的切換

3.UIViewController生命週期

4.多個Controller之間的資料如何交換

 

=========================

UIViewController

 

   1.MVC設計模式

MVC設計模式:Model模型 View視圖 Controller控制器

 

   2.window需要一個根視圖控制器

RootViewController *rootCtl = [[RootViewController alloc] init];

    

    //設定根視圖控制器

    //如果在不更換根視圖控制器的情況下,根視圖控制器是不會被釋放掉的

    self.window.rootViewController = rootCtl;

 

   3.UIViewController對象預設都會有一個內建的view

//執行個體化一個視圖控制器

    UIViewController *ctl = [[UIViewController alloc] init];

    //每一個控制器上,都內建了一個UIView

    ctl.view.alpha = 0.5;

    

    //設定控制器內建View的背景色(預設無背景色)

    ctl.view.backgroundColor = [UIColor whiteColor];

    self.window.backgroundColor = [UIColor orangeColor];

 

   4.UIViewController和UIView的關係  

     1)UIViewController是視圖控制器,而UIView是視圖,也就是說UIViewController控制UIView 

    2)UIView需要顯示的內容(資料)也是要通過UIViewController來管理

     例子:UIViewController就是一個相框,而UIView就是一個相片

       相框可以隨時隨地的拿走這個相片而換另外一張相片,或者在這張相片上加一個新的相片

       而相片卻不能操縱相框 

 

=========================

UIViewController的基本用法

 

   1.執行個體化UIViewController 時調用init方法後,會自動跳轉到如下方法

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil//控制器的初始化方法//init - > initWithNibName:nibNameOrNil bundle:-(instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil{    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {        //注意: 不允許執行跟UI有關係的操作,雖然執行有些沒報錯,但這樣既不嚴格也容易出現一些亂七八糟的問題        //self.view.backgroundColor XXX        //允許執行跟資料有關係的操作        //例如:數組的執行個體化,字典的執行個體化等等      }    return self;}

 

 

   2.viewDidLoad的作用

     一般我們會在這裡做介面上的做一些額外操作,比如往view中添加一些子視圖、

     從資料庫或者網路載入模型資料裝配到子視圖中

 

   3.重寫viewDidLoad方法

 

=========================

UIViewController之間的切換

 

   1.分割功能到不同的Controller頁面上

 

   2.如何管理多個UIViewController之間的切換

     1)切換到新的視圖控制器

      - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion

//切換到新的控制器

    /*

     參數1:要切換至的控制器對象

     參數2:是否需要動畫

     參數3:回調block,一般為nil

     */

    

    //懶載入機制:這個對象需要使用的時候,才會去執行個體化

    

[self presentViewController:newCtl animated:YES completion:nil];

 

 

     2)關閉當前視圖控制器

      - (void)dismissViewControllerAnimated:(BOOL)flag 

        completion:(void (^)(void))completion

[self dismissViewControllerAnimated:YES completion:nil];

 

 

     3)模式切換方式:

modalTransitionStyle

 

UIModalTransitionStyleCoverVertical    從下向上

UIModalTransitionStyleCrossDissolve      漸層

UIModalTransitionStyleFlipHorizontal     橫向翻轉

UIModalTransitionStylePartialCurl           翻書

//設定切換的動畫效果

    

newCtl.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;

 

 

=========================

UIViewController的生命週期

 順序:

   1.alloc建立對象,分配空間

   2.init初始化對象,初始化資料

   3.LoadView -- self.view的建立 

   4.viewDidLoad作用

   5.viewDidLoad何時被調用

   6.viewWillAppear函數

   7.viewDidAppear函數

   8.viewWillDisappear函數

   9.viewDidDisappear函數

   10.dealloc視圖被銷毀

   //視圖即將消失

例子:

-(void)viewWillDisappear:(BOOL)animated{    [super viewWillDisappear:animated];    NSLog(@"viewWillDisappear");}

 

 

=========================

多個Controller之間的資料如何交換

   

     1.正向傳值

VA —> VB

傳值一方:

例如RootViewController.m裡傳值

NewViewController *newCtl = [NewViewController new];

    

    UILabel *label = (UILabel *)[self.view viewWithTag:100];

  1: newCtl.deliverStr = label.text;

擷取值一方:

   2:  NewViewController.h :  @property (nonatomic,copy)NSString *deliverStr;//接受傳遞過來的參數

   3:  NewViewController.m: label.text = self.deliverStr;

 

     2.反向傳值

VA <— VB

傳值一方

1:typedef void (^blockType)(NSString *);

 

@interface NewViewController : UIViewController

//回調的block

2:@property (nonatomic,copy)blockType block;

 

 

傳值一方

NewViewController *newCtl = [NewViewController new];

    

    UILabel *label = (UILabel *)[self.view viewWithTag:100];

//設定block的主體實現

  3:  newCtl.block = ^(NSString *text){

        label.text = text;      

    };

 

3.通過AppDelegate對象傳值

1)擷取當前應用的sharedApplication對象:

       

+ (UIApplication *)sharedApplication 傳值一方UILabel *label = (UILabel *)[self.view viewWithTag:100];        //先給AppDelgate的中轉變數進行賦值        //1.擷取UIAplication對象    //2.擷取AppDelgate對象    //3.賦值    UIApplication *app = [UIApplication sharedApplication];    AppDelegate *del = app.delegate;    del.middleStr = label.text;

 

 

       2)擷取當前應用的AppDelegate對象:

       @property(nonatomic,assign) id<UIApplicationDelegate> delegate;

 

AppDelegate.h 裡

//中轉的屬性變數

@property (nonatomic,copy)NSString *middleStr;

 

 

3)強制轉換為AppDelegate對象,然後通過該對象的setter和getter傳值

取值一方

例如在將出現的函數裡調用

-(void)viewWillAppear:(BOOL)animated{    [super viewWillAppear:animated];      UIApplication *app = [UIApplication sharedApplication];    AppDelegate *del = app.delegate;    //判斷不為空白    if (del.middleStr.length!=0) {        UILabel *label = (UILabel *)[self.view viewWithTag:100];        label.text = del.middleStr;    }}

 

 

協議代理傳值方法

先建立一個協議.h檔案,即 protocol 檔案:內容如

#import <Foundation/Foundation.h>@protocol Myprotocol <NSObject>//代理需要完成的任務-(void)showInfo:(NSString *)info;@end

 

傳值一方:遵守協議

.h 檔案:

@property (nonatomic,weak)id<Myprotocol> delegate;//指向代理對象

 

.m檔案:

//讓代理對象執行方法

   

if ([_delegate respondsToSelector:@selector(showInfo:)]) {        [_delegate showInfo:label.text];    }  

 

 

    //協議代理

    //代理:能完成這個任務的對象

    //被代理:希望完成這個任務,但是本身不能實現的對象

    //設定代理:找到能同時擷取到代理和被代理對象的位置

 

擷取值一方:遵守協議

.h檔案裡:

@interface RootViewController : UIViewController<Myprotocol>

 

.m檔案裡:

-(void)showInfo:(NSString *)info{    //重新整理Label顯示的文字內容    UILabel *label = (UILabel *)[self.view viewWithTag:100];    label.text = info;  }

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.