亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

iOS開發(fā)中導(dǎo)航控制器的基本使用教程

 更新時(shí)間:2015年11月27日 09:37:02   作者:文頂頂  
這篇文章主要介紹了iOS開發(fā)中導(dǎo)航控制器的基本使用教程,代碼基于傳統(tǒng)的Objective-C,需要的朋友可以參考下

多控制器和導(dǎo)航控制器簡(jiǎn)單介紹
一、多控制器

一個(gè)iOS的app很少只由一個(gè)控制器組成,除非這個(gè)app極其簡(jiǎn)單。當(dāng)app中有多個(gè)控制器的時(shí)候,我們就需要對(duì)這些控制器進(jìn)行管理

有多個(gè)view時(shí),可以用一個(gè)大的view去管理1個(gè)或者多個(gè)小view,控制器也是如此,用1個(gè)控制器去管理其他多個(gè)控制器

比如,用一個(gè)控制器A去管理3個(gè)控制器B、C、D??刂破鰽被稱為控制器B、C、D的“父控制器”;控制器B、C、D的被稱為控制器A的“子控制器”

為了便于管理控制器,iOS提供了2個(gè)比較特殊的控制器

  • UINavigationController
  • UITabBarController

二、導(dǎo)航控制器

利用UINavigationController,可以輕松地管理多個(gè)控制器,輕松完成控制器之間的切換,典型例子就是系統(tǒng)自帶的“設(shè)置”應(yīng)用

如圖:

2015112793127742.png (633×266)

三、UINavigationController的使用步驟

(1)初始化UINavigationController

(2)設(shè)置UIWindow的rootViewController為UINavigationController

(3)根據(jù)具體情況,通過push方法添加對(duì)應(yīng)個(gè)數(shù)的子控制器

復(fù)制代碼 代碼如下:

#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.創(chuàng)建一個(gè)導(dǎo)航控制器
    UINavigationController *nav=[[UINavigationController alloc]init];
    //2.設(shè)置導(dǎo)航控制器為window的根視圖
    self.window.rootViewController=nav;

   
    //3.添加子控制器到導(dǎo)航控制器中
    //創(chuàng)建一些控制器
    UIViewController *c1=[[UIViewController alloc]init];
    //設(shè)置c1這個(gè)控制器的視圖顏色
    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];
   
//把這些控制器添加到導(dǎo)航控制器中
    [nav pushViewController:c1 animated:YES];
    [nav pushViewController:c2 animated:YES];
    [nav pushViewController:c3 animated:YES];
   
    [self.window makeKeyAndVisible];
    return YES;
}


運(yùn)行模擬器,可以看到一個(gè)簡(jiǎn)陋的有著三個(gè)子控制器管理著頁面。

但呈現(xiàn)在我們眼前的只能有一個(gè)界面,我們沒有必要一次性創(chuàng)建三個(gè)控制器在這里等著。

要求:創(chuàng)建三個(gè)子控制器,每個(gè)子控制器view的界面上放一個(gè)按鈕,點(diǎn)擊可以跳轉(zhuǎn)到下一個(gè)界面。

實(shí)現(xiàn)(完成三個(gè)頁面間通過按鈕進(jìn)行簡(jiǎn)單的跳轉(zhuǎn)):

說明:這里把第一個(gè)子控制器的創(chuàng)建等代碼寫在了代理方法中。

YYAppDelegate.m文件代碼

復(fù)制代碼 代碼如下:

//
//  YYAppDelegate.m
//  01-導(dǎo)航控制器的使用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.創(chuàng)建一個(gè)導(dǎo)航控制器
    UINavigationController *nav=[[UINavigationController alloc]init];
    //2.設(shè)置導(dǎo)航控制器為window的根視圖
    self.window.rootViewController=nav;

   
    //3.添加子控制器到導(dǎo)航控制器中
    YYOneViewController *one=[[YYOneViewController alloc]init];
    [nav pushViewController:one animated:YES];
   
    [self.window makeKeyAndVisible];
    return YES;
   
   
//    //創(chuàng)建一些控制器
//    UIViewController *c1=[[UIViewController alloc]init];
//    //設(shè)置c1這個(gè)控制器的視圖顏色
//    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];
//   
////把這些控制器添加到導(dǎo)航控制器中
//    [nav pushViewController:c1 animated:YES];
//    [nav pushViewController:c2 animated:YES];
//    [nav pushViewController:c3 animated:YES];
}


創(chuàng)建三個(gè)子控件類及對(duì)應(yīng)的xib文件
復(fù)制代碼 代碼如下:

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

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

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

@end


復(fù)制代碼 代碼如下:

@implementation YYOneViewController


- (IBAction)jump2two:(id)sender {
    //1.創(chuàng)建第二個(gè)子控制器
    YYTwoViewController *two=[[YYTwoViewController alloc]init];
   
    //2.把子控制器添加到導(dǎo)航控制器中
    //有什么辦法能夠拿到導(dǎo)航控制器?
     //只要當(dāng)前控制器是導(dǎo)航控制器的子控制器,那么就可以通過該屬性直接獲取到當(dāng)前控制器所在的導(dǎo)航控制器
    [self.navigationController pushViewController:two animated:YES];
}
@end


復(fù)制代碼 代碼如下:

YYTwoViewController.m文件
//
//  YYTwoViewController.m
//  01-導(dǎo)航控制器的使用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


復(fù)制代碼 代碼如下:

@implementation YYTwoViewController

//跳轉(zhuǎn)到第三個(gè)子控制器
- (IBAction)jump2Three:(id)sender {
    //1.創(chuàng)建第三個(gè)子控制器
    YYThreeViewController *three=[[YYThreeViewController alloc]init];
    //2.將子控制器添加到導(dǎo)航控制器中
    [self.navigationController pushViewController:three animated:YES];
   
}
@end


示:只要當(dāng)前控制器是導(dǎo)航控制器的子控制器,那么就可以通過self.navigationController屬性直接獲取到當(dāng)前控制器所在的導(dǎo)航控制器

項(xiàng)目文件結(jié)構(gòu)和運(yùn)行效果:

2015112793411187.png (585×502)

導(dǎo)航控制器屬性和基本使用
一、導(dǎo)航控制器的一些屬性和基本使用

1.把子控制器添加到導(dǎo)航控制器中的四種方法

(1)

 1.創(chuàng)建一個(gè)導(dǎo)航控制器

復(fù)制代碼 代碼如下:

    UINavigationController *nav=[[UINavigationControlleralloc]init];

2.設(shè)置導(dǎo)航控制器為window的根視圖

復(fù)制代碼 代碼如下:

    self.window.rootViewController=nav;

3.添加

復(fù)制代碼 代碼如下:

    YYOneViewController  *one = [[YYOneViewController  alloc] init];

    [nav pushViewController:one animated:YES];

(2)

 1.創(chuàng)建一個(gè)導(dǎo)航控制器

復(fù)制代碼 代碼如下:

       UINavigationController *nav=[[UINavigationControlleralloc]init];

 2.設(shè)置導(dǎo)航控制器為window的根視圖
復(fù)制代碼 代碼如下:

 self.window.rootViewController=nav;

 3.添加
復(fù)制代碼 代碼如下:

YYOneViewController  *one = [[YYOneViewController  alloc] init];

 [nav addChildViewController:one];

(3)

 1.創(chuàng)建一個(gè)導(dǎo)航控制器

復(fù)制代碼 代碼如下:

       UINavigationController *nav=[[UINavigationControlleralloc]init];

 2.設(shè)置導(dǎo)航控制器為window的根視圖
復(fù)制代碼 代碼如下:

 self.window.rootViewController=nav;

3.添加
復(fù)制代碼 代碼如下:

YYOneViewController  *one = [[YYOneViewController  alloc] init];

nav.viewControllers=@[one];

(添加到導(dǎo)航控制器的棧中)

說明:

復(fù)制代碼 代碼如下:
nav.viewControllers;== nav.childViewControllers;
注意該屬性是只讀的,因此不能像下面這樣寫。
復(fù)制代碼 代碼如下:
nav.childViewControllers = @[one];

(4)最常用的方法

復(fù)制代碼 代碼如下:

 YYOneViewController *one=[[YYOneViewController alloc]init];

 UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];


 

2.當(dāng)前子控制器界面導(dǎo)航欄的標(biāo)題以及對(duì)應(yīng)返回標(biāo)題的設(shè)置

復(fù)制代碼 代碼如下:

    self.navigationItem.title=@"第一個(gè)界面";

    self.navigationItem.backBarButtonItem=[[UIBarButtonItemalloc]initWithTitle:@"返回一" style:UIBarButtonItemStylePlain target:nilaction:nil];


3.給導(dǎo)航欄添加按鈕

說明:可添加一個(gè),也可以添加多個(gè)(數(shù)組)

   添加導(dǎo)航欄左邊的按鈕(添加一個(gè)相機(jī)圖標(biāo)的按鈕),會(huì)蓋掉返回

復(fù)制代碼 代碼如下:

    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];

4.界面跳轉(zhuǎn)

跳轉(zhuǎn)到第二個(gè)界面(當(dāng)前為第三個(gè),移除當(dāng)前棧頂?shù)目刂破?

 

復(fù)制代碼 代碼如下:

[self.navigationControllerpopViewControllerAnimated:YES];


   移除處理?xiàng)5卓刂破髦獾乃锌刂破?nbsp;

復(fù)制代碼 代碼如下:

 [self.navigationControllerpopToRootViewControllerAnimated:YES];


  只要傳入棧中的某一個(gè)控制器,就會(huì)跳轉(zhuǎn)到指定控制器

復(fù)制代碼 代碼如下:

[self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];


二、代碼示例

YYAppDelegate.m文件

復(fù)制代碼 代碼如下:

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

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

@implementation YYAppDelegate

//應(yīng)用程序啟動(dòng)完畢即會(huì)調(diào)用
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
   
   
    //3.添加子控制器到導(dǎo)航控制器中
    //第一種也是最常用的一種
//    YYOneViewController *one=[[YYOneViewController alloc]init];
//    UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];
   
    //1.創(chuàng)建一個(gè)導(dǎo)航控制器
    UINavigationController *nav=[[UINavigationController alloc]init];
    //2.設(shè)置導(dǎo)航控制器為window的根視圖
    self.window.rootViewController=nav;
   
    //第二種
    YYOneViewController  *one = [[YYOneViewController  alloc] init];
    [nav pushViewController:one animated:YES];
   
    //第三種
//    [nav addChildViewController:one];
//    第四種(添加到導(dǎo)航控制器的棧中)
//    nav.viewControllers=@[one];
   
    // 導(dǎo)航控制器的棧
    //    nav.viewControllers;== nav.childViewControllers;
    // 注意該屬性是只讀的,因此不能像下面這樣寫
    //    nav.childViewControllers = @[one];
   
   
    [self.window makeKeyAndVisible];
    return YES;
}

@end


YYOneViewController.m文件
復(fù)制代碼 代碼如下:

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

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

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

@end


復(fù)制代碼 代碼如下:

@implementation YYOneViewController


- (IBAction)jump2two:(id)sender {
    //1.創(chuàng)建第二個(gè)子控制器
    YYTwoViewController *two=[[YYTwoViewController alloc]init];
   
    //2.把子控制器添加到導(dǎo)航控制器中
    //有什么辦法能夠拿到導(dǎo)航控制器?
     //只要當(dāng)前控制器是導(dǎo)航控制器的子控制器,那么就可以通過該屬性直接獲取到當(dāng)前控制器所在的導(dǎo)航控制器
    [self.navigationController pushViewController:two animated:YES];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    //控制當(dāng)前控制器對(duì)應(yīng)的導(dǎo)航條顯示的內(nèi)容
    self.navigationItem.title=@"第一個(gè)界面";
    //修改返回按鈕顯示的內(nèi)容
    self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回一" style:UIBarButtonItemStylePlain target:nil action:nil];
}
@end


YYTwoViewController.m文件
復(fù)制代碼 代碼如下:

//
//  YYTwoViewController.m
//  01-導(dǎo)航控制器的使用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


復(fù)制代碼 代碼如下:

@implementation YYTwoViewController

//跳轉(zhuǎn)到第三個(gè)子控制器
- (IBAction)jump2Three:(id)sender {
    //1.創(chuàng)建第三個(gè)子控制器
    YYThreeViewController *three=[[YYThreeViewController alloc]init];
    //2.將子控制器添加到導(dǎo)航控制器中
    [self.navigationController pushViewController:three animated:YES];
}

-(void)viewDidLoad
{
    [super viewDidLoad];
    //給導(dǎo)航欄添加按鈕
    //添加導(dǎo)航欄左邊的按鈕(添加一個(gè)相機(jī)圖標(biāo)的按鈕),會(huì)蓋掉返回
//    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
   
    //為導(dǎo)航欄在右邊添加多個(gè)按鈕
    //創(chuàng)建兩個(gè)按鈕
    UIBarButtonItem *a=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemStop target:nil action:nil];
    UIBarButtonItem *b=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:nil action:nil];
    UIBarButtonItem *c=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];
    self.navigationItem.rightBarButtonItems=@[a,b,c];
   
    //設(shè)置對(duì)應(yīng)的導(dǎo)航條的返回(第三個(gè)界面導(dǎo)航條的返回)
    self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
}
@end


YYThreeViewController.m文件
復(fù)制代碼 代碼如下:

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

#import "YYThreeViewController.h"
#import "YYTwoViewController.h"

@interface YYThreeViewController ()
//返回到第二個(gè)控制器頁面
- (IBAction)jump2two:(id)sender;
//返回到第一個(gè)控制器頁面
- (IBAction)jump2root:(id)sender;

@end


復(fù)制代碼 代碼如下:

@implementation YYThreeViewController


- (IBAction)jump2two:(id)sender {
    //跳轉(zhuǎn)到第二個(gè)界面(移除當(dāng)前棧頂?shù)目刂破?
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)jump2root:(id)sender {
    //移除處理?xiàng)5卓刂破髦獾乃锌刂破?br />     [self.navigationController popToRootViewControllerAnimated:YES];
   
    // 只要傳入棧中的某一個(gè)控制器,就會(huì)跳轉(zhuǎn)到指定控制器
       //不能這樣,沒添加到導(dǎo)航控制器YYTwoViewController *two = [[YYTwoViewController  alloc] init];
    //[self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];
}
@end


實(shí)現(xiàn)效果:

2015112793512974.png (317×500)

2015112793526599.png (317×500)

2015112793540101.png (318×499)

三、導(dǎo)航控制器通過棧來管理子控制器

示意圖

2015112793557017.png (679×346)

說明:

導(dǎo)航控制器是通過棧的形式來管理子控制器的(先進(jìn)后出)

顯示在導(dǎo)航控制器上得view永遠(yuǎn)是棧頂控制器的view

一個(gè)導(dǎo)航控制器只有一個(gè)導(dǎo)航條,也就是說所有的自控制器公用一個(gè)導(dǎo)航條。

四、補(bǔ)充

在代理方法中,打印當(dāng)前window下面的所有子控件,并通過xml文件來保存,代碼如下。

復(fù)制代碼 代碼如下:

// 應(yīng)用程序獲取焦點(diǎn)(代表著可以和用戶交互)
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    NSLog(@"applicationDidBecomeActive");
   
   
    UINavigationController *nav =  (UINavigationController *)self.window.rootViewController;
    UINavigationBar *bar =  nav.navigationBar;
//    NSLog(@"%@", NSStringFromCGRect(bar.frame));
   
    NSString *str =  [self digView:self.window];
    [str writeToFile:@"/Users/apple/Desktop/ios6.xml" atomically:YES];
   
}

/**
 *  返回傳入veiw的所有層級(jí)結(jié)構(gòu)
 *
 *  @param view 需要獲取層級(jí)結(jié)構(gòu)的view
 *
 *  @return 字符串
 */
- (NSString *)digView:(UIView *)view
{
    if ([view isKindOfClass:[UITableViewCell class]]) return @"";
    // 1.初始化
    NSMutableString *xml = [NSMutableString string];
   
    // 2.標(biāo)簽開頭
    [xml appendFormat:@"<%@ frame=\"%@\"", view.class, NSStringFromCGRect(view.frame)];
    if (!CGPointEqualToPoint(view.bounds.origin, CGPointZero)) {
        [xml appendFormat:@" bounds=\"%@\"", NSStringFromCGRect(view.bounds)];
    }
   
    if ([view isKindOfClass:[UIScrollView class]]) {
        UIScrollView *scroll = (UIScrollView *)view;
        if (!UIEdgeInsetsEqualToEdgeInsets(UIEdgeInsetsZero, scroll.contentInset)) {
            [xml appendFormat:@" contentInset=\"%@\"", NSStringFromUIEdgeInsets(scroll.contentInset)];
        }
    }
   
    // 3.判斷是否要結(jié)束
    if (view.subviews.count == 0) {
        [xml appendString:@" />"];
        return xml;
    } else {
        [xml appendString:@">"];
    }
   
    // 4.遍歷所有的子控件
    for (UIView *child in view.subviews) {
        NSString *childXml = [self digView:child];
        [xml appendString:childXml];
    }
   
    // 5.標(biāo)簽結(jié)尾
    [xml appendFormat:@"</%@>", view.class];
   
    return xml;
}


注意:在ios7和以前版本中,各個(gè)控件,包括子控制器界面frame的不同。

相關(guān)文章

最新評(píng)論