iOS開發UI篇—多控制器和導航控制器簡單介紹

iOS開發UI篇—多控制器和導航控制器簡單介紹app

1、多控制器spa

一個iOS的app不多隻由一個控制器組成,除非這個app極其簡單。當app中有多個控制器的時候,咱們就須要對這些控制器進行管理代理

有多個view時,能夠用一個大的view去管理1個或者多個小view,控制器也是如此,用1個控制器去管理其餘多個控制器code

好比,用一個控制器A去管理3個控制器B、C、D。控制器A被稱爲控制器B、C、D的「父控制器」;控制器B、C、D的被稱爲控制器A的「子控制器」blog

爲了便於管理控制器,iOS提供了2個比較特殊的控制器three

UINavigationController開發

UITabBarControllerit

2、導航控制器io

利用UINavigationController,能夠輕鬆地管理多個控制器,輕鬆完成控制器之間的切換,典型例子就是系統自帶的「設置」應用class

如圖:

3、UINavigationController的使用步驟

(1)初始化UINavigationController

(2)設置UIWindow的rootViewController爲UINavigationController

(3)根據具體狀況,經過push方法添加對應個數的子控制器

複製代碼

#import "YYAppDelegate.h"
#import "YYOneViewController.h"

@implementation YYAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];

//1.建立一個導航控制器
UINavigationController *nav=[[UINavigationController alloc]init];
//2.設置導航控制器爲window的根視圖
self.window.rootViewController=nav;


//3.添加子控制器到導航控制器中
//建立一些控制器
UIViewController *c1=[[UIViewController alloc]init];
//設置c1這個控制器的視圖顏色
c1.view.backgroundColor=[UIColor redColor];

UIViewController *c2=[[UIViewController alloc]init];
c2.view.backgroundColor=[UIColor purpleColor];

UIViewController *c3=[[UIViewController alloc]init];
c3.view.backgroundColor=[UIColor brownColor];

//把這些控制器添加到導航控制器中
[nav pushViewController:c1 animated:YES];
[nav pushViewController:c2 animated:YES];
[nav pushViewController:c3 animated:YES];

[self.window makeKeyAndVisible];
return YES;
}

 
 
複製代碼

運行模擬器,能夠看到一個簡陋的有着三個子控制器管理着頁面。

但呈如今咱們眼前的只能有一個界面,咱們沒有必要一次性建立三個控制器在這裏等着。

要求:建立三個子控制器,每一個子控制器view的界面上放一個按鈕,點擊能夠跳轉到下一個界面。

實現(完成三個頁面間經過按鈕進行簡單的跳轉):

說明:這裏把第一個子控制器的建立等代碼寫在了代理方法中。

YYAppDelegate.m文件代碼

複製代碼

//
// YYAppDelegate.m
// 01-導航控制器的使用1
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYAppDelegate.h"
#import "YYOneViewController.h"

@implementation YYAppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
self.window.backgroundColor = [UIColor whiteColor];

//1.建立一個導航控制器
UINavigationController *nav=[[UINavigationController alloc]init];
//2.設置導航控制器爲window的根視圖
self.window.rootViewController=nav;


//3.添加子控制器到導航控制器中
YYOneViewController *one=[[YYOneViewController alloc]init];
[nav pushViewController:one animated:YES];

[self.window makeKeyAndVisible];
return YES;


// //建立一些控制器
// UIViewController *c1=[[UIViewController alloc]init];
// //設置c1這個控制器的視圖顏色
// c1.view.backgroundColor=[UIColor redColor];
//
// UIViewController *c2=[[UIViewController alloc]init];
// c2.view.backgroundColor=[UIColor purpleColor];
//
// UIViewController *c3=[[UIViewController alloc]init];
// c3.view.backgroundColor=[UIColor brownColor];
//
////把這些控制器添加到導航控制器中
// [nav pushViewController:c1 animated:YES];
// [nav pushViewController:c2 animated:YES];
// [nav pushViewController:c3 animated:YES];
}

複製代碼

建立三個子控件類及對應的xib文件

YYOneViewController.m文件

複製代碼

//
// YYOneViewController.m
// 01-導航控制器的使用1
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYOneViewController.h"
#import "YYTwoViewController.h"

@interface YYOneViewController ()
/**
跳轉到第二個界面
*/
- (IBAction)jump2two:(id)sender;

@end

@implementation YYOneViewController


- (IBAction)jump2two:(id)sender {
//1.建立第二個子控制器
YYTwoViewController *two=[[YYTwoViewController alloc]init];

//2.把子控制器添加到導航控制器中
//有什麼辦法可以拿到導航控制器?
//只要當前控制器是導航控制器的子控制器,那麼就能夠經過該屬性直接獲取到當前控制器所在的導航控制器
[self.navigationController pushViewController:two animated:YES];
}
@end

 
 
複製代碼

YYTwoViewController.m文件

複製代碼

//
// YYTwoViewController.m
// 01-導航控制器的使用1
//
// Created by apple on 14-6-4.
// Copyright (c) 2014年 itcase. All rights reserved.
//

#import "YYTwoViewController.h"
#import "YYThreeViewController.h"
@interface YYTwoViewController ()
- (IBAction)jump2Three:(id)sender;

@end

@implementation YYTwoViewController

//跳轉到第三個子控制器
- (IBAction)jump2Three:(id)sender {
//1.建立第三個子控制器
YYThreeViewController *three=[[YYThreeViewController alloc]init];
//2.將子控制器添加到導航控制器中
[self.navigationController pushViewController:three animated:YES];

}
@end

 
 
複製代碼

提示:只要當前控制器是導航控制器的子控制器,那麼就能夠經過self.navigationController屬性直接獲取到當前控制器所在的導航控制器

項目文件結構和運行效果:

相關文章
相關標籤/搜索