iOS應用程式之控制器管理

來源:互聯網
上載者:User

標籤:style   blog   http   color   io   os   ar   使用   sp   

  • 控制器以及view的多種建立方式
  • UINavigationController的簡單使用:添加\移除子控制器
  • UINavigationBar內容的設定
  • 控制器的生命週期方法

一、控制器以及view的多種建立方式

1??控制器的建立方式

-直接建立

ViewController *test = [[ViewController alloc] init];

-指定xib檔案來建立

ViewController *test = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];

-通過storyboard建立

先載入storyboard檔案(Test是storyboard的檔案名稱)
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Test" bundle:nil];
•接著初始化storyboard中的控制器初始化“初始控制器”(箭頭所指的控制器)
ViewController *test = [storyboard instantiateInitialViewController];
通過一個標識初始化對應的控制器
ViewController *mj = [storyboard instantiateViewControllerWithIdentifier:@”test"];

2??View載入的方式(MJ老師的一個圖就明白了)

•控制器的view是消極式載入的:用到時再載入•可以用isViewLoaded方法判斷一個UIViewController的view是否已經被載入•控制器的view載入完畢就會調用viewDidLoad方法

二、UINavigationController的簡單使用:添加\移除子控制器

1??利用UINavigationController,可以輕鬆地管理多個控制器,輕鬆完成控制器之間的切換,典型例子就是系統內建的“設定”應用2??UINavigationController的使用步驟-初始化UINavigationController-設定UIWindow的rootViewController為UINavigationController-根據具體情況,通過push方法添加對應個數的子控制器三、UINavigationBar內容的設定•UINavigationController以棧的形式儲存子控制器
@property(nonatomic,copy) NSArray *viewControllers;@property(nonatomic,readonly) NSArray *childViewControllers;
•使用push方法能將某個控制器壓入棧
- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated;
•使用pop方法可以移除控制器將棧頂的控制器移除
- (UIViewController *)popViewControllerAnimated:(BOOL)animated;
回到指定的子控制器
- (NSArray *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated;
回到根控制器(棧底控制器)
- (NSArray *)popToRootViewControllerAnimated:(BOOL)animated;

改變內容

//UINavigationItem有以下屬性影響著導覽列的內容//左上方的返回按鈕@property(nonatomic,retain) UIBarButtonItem *backBarButtonItem;//中間的標題視圖@property(nonatomic,retain) UIView          *titleView;//中間的標題文字@property(nonatomic,copy)   NSString        *title;//左上方的視圖@property(nonatomic,retain) UIBarButtonItem *leftBarButtonItem;//UIBarButtonItem *rightBarButtonItem  右上方的視圖@property(nonatomic,retain) UIBarButtonItem *rightBarButtonItem;

四、ViewController的生命週期

tep1:initWithNib             viewController 會進行alloc,並init.

step2: loadView               在這裡會看它的子類是否有重寫這個函數,如果重寫了則調用子類的,否則就調用她自己的。注意,這個時候視圖還是沒有沒載入進來的哦。

step3:viewDidLoad         這個時候視圖已經存在了。可以在這裡添加你想要添加的UI控制項了。

step4:viewWillAppear     視圖將出現在螢幕上了

step5:viewDidAppear       視圖已經成功在螢幕上渲染完成了

step6:viewWillDisappear   視圖將要消失了

step7:viewDidDisappear   視圖從螢幕上消失了

step8:viewDidUnLoad       當發生記憶體警告的時候,如果本視圖不是當前正在顯示的視圖,則會執行這個函數。將子視圖釋放。

step9:dealloc                   釋放viewController

而view的生命週期則是step3_step8.

iOS應用程式之控制器管理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.