iOS開發UI篇—導航控制器屬性和基本使用

iOS開發UI篇—導航控制器屬性和基本使用  ios

1、導航控制器的一些屬性和基本使用數組

1.把子控制器添加到導航控制器中的四種方法app

(1)atom

 1.建立一個導航控制器spa

    UINavigationController *nav=[[UINavigationControlleralloc]init];代理

2.設置導航控制器爲window的根視圖code

    self.window.rootViewController=nav;orm

3.添加xml

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

    [nav pushViewController:one animated:YES];

(2)

 1.建立一個導航控制器

       UINavigationController *nav=[[UINavigationControlleralloc]init];

 2.設置導航控制器爲window的根視圖

 self.window.rootViewController=nav;

 3.添加

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

 [nav addChildViewController:one];

(3)

 1.建立一個導航控制器

       UINavigationController *nav=[[UINavigationControlleralloc]init];

 2.設置導航控制器爲window的根視圖

 self.window.rootViewController=nav;

3.添加

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

nav.viewControllers=@[one];(添加到導航控制器的棧中)

說明:nav.viewControllers;== nav.childViewControllers;注意該屬性是隻讀的,所以不能像下面這樣寫。nav.childViewControllers = @[one];

(4)最經常使用的方法

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

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

 

2.當前子控制器界面導航欄的標題以及對應返回標題的設置

    self.navigationItem.title=@"第一個界面";

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

3.給導航欄添加按鈕

說明:可添加一個,也能夠添加多個(數組)

   添加導航欄左邊的按鈕(添加一個相機圖標的按鈕),會蓋掉返回

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

4.界面跳轉

跳轉到第二個界面(當前爲第三個,移除當前棧頂的控制器) [self.navigationControllerpopViewControllerAnimated:YES];

   移除處理棧底控制器以外的全部控制器  [self.navigationControllerpopToRootViewControllerAnimated:YES];

  只要傳入棧中的某一個控制器,就會跳轉到指定控制器 [self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];

2、代碼示例

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];


//3.添加子控制器到導航控制器中
//第一種也是最經常使用的一種
// YYOneViewController *one=[[YYOneViewController alloc]init];
// UINavigationController *nav=[[UINavigationController alloc]initWithRootViewController:one];

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

//第二種
YYOneViewController *one = [[YYOneViewController alloc] init];
[nav pushViewController:one animated:YES];

//第三種
// [nav addChildViewController:one];
// 第四種(添加到導航控制器的棧中)
// nav.viewControllers=@[one];

// 導航控制器的棧
// nav.viewControllers;== nav.childViewControllers;
// 注意該屬性是隻讀的,所以不能像下面這樣寫
// nav.childViewControllers = @[one];


[self.window makeKeyAndVisible];
return YES;
}

@end

 
 
複製代碼

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];
}

-(void)viewDidLoad
{
[super viewDidLoad];
//控制當前控制器對應的導航條顯示的內容
self.navigationItem.title=@"第一個界面";
//修改返回按鈕顯示的內容
self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回一" style:UIBarButtonItemStylePlain target:nil action:nil];
}
@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];
}

-(void)viewDidLoad
{
[super viewDidLoad];
//給導航欄添加按鈕
//添加導航欄左邊的按鈕(添加一個相機圖標的按鈕),會蓋掉返回
// self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:nil action:nil];

//爲導航欄在右邊添加多個按鈕
//建立兩個按鈕
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];

//設置對應的導航條的返回(第三個界面導航條的返回)
self.navigationItem.backBarButtonItem=[[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStyleBordered target:nil action:nil];
}
@end

複製代碼

YYThreeViewController.m文件

複製代碼

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

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

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

@end

@implementation YYThreeViewController


- (IBAction)jump2two:(id)sender {
//跳轉到第二個界面(移除當前棧頂的控制器)
[self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)jump2root:(id)sender {
//移除處理棧底控制器以外的全部控制器
[self.navigationController popToRootViewControllerAnimated:YES];

// 只要傳入棧中的某一個控制器,就會跳轉到指定控制器
//不能這樣,沒添加到導航控制器YYTwoViewController *two = [[YYTwoViewController alloc] init];
//[self.navigationController popToViewController:<#(UIViewController *)#> animated:<#(BOOL)#>];
}
@end

複製代碼

實現效果:

       

3、導航控制器經過棧來管理子控制器

示意圖

說明:

導航控制器是經過棧的形式來管理子控制器的(先進後出)

顯示在導航控制器上得view永遠是棧頂控制器的view

一個導航控制器只有一個導航條,也就是說全部的自控制器公用一個導航條。

 

4、補充

在代理方法中,打印當前window下面的全部子控件,並經過xml文件來保存,代碼以下。

複製代碼

// 應用程序獲取焦點(表明着能夠和用戶交互)
- (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的全部層級結構
*
* @param view 須要獲取層級結構的view
*
* @return 字符串
*/
- (NSString *)digView:(UIView *)view
{
if ([view isKindOfClass:[UITableViewCell class]]) return @"";
// 1.初始化
NSMutableString *xml = [NSMutableString string];

// 2.標籤開頭
[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.判斷是否要結束
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.標籤結尾
[xml appendFormat:@"</%@>", view.class];

return xml;
}

 
 
複製代碼

注意:在ios7和之前版本中,各個控件,包括子控制器界面frame的不一樣。

相關文章
相關標籤/搜索