您的位置:首页 > 产品设计 > UI/UE

UINavigationController

2015-12-14 22:49 375 查看
//AppDelegate.m

- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

self.window = [[UIWindowalloc]initWithFrame:[[UIScreenmainScreen] bounds]];

// Override point for customization afterapplication launch.

self.window.backgroundColor =[UIColorwhiteColor];

   [self.windowmakeKeyAndVisible];

RootViewController *rootViewController =[[RootViewControlleralloc]init];

  //  创建导航控制器(将rootViewController作为导航控制器的根视图控制器)

UINavigationController *navC =[[UINavigationControlleralloc]initWithRootViewController:rootViewController];

   // 将导航控制器作为window根视图控制器

self.window.rootViewController = navC;

 

return YES;

}

 

 

//

//  RootViewController.m

 

//UINavigationControllerUINavigationController:导航控制器,是iOS中最常用的多视图控制器之一,它用来管理多个视图控制器。导航控制器可以认为是管理控制器的控制器,主要管理有层级关系的控制器。(导航控制器的导航栏共用同一个)

#import "RootViewController.h"

#import "SecondViewController.h"

 

@interfaceRootViewController ()

 

@end

 

@implementationRootViewController

 

- (void)viewDidLoad {

   [superviewDidLoad];

 

   // 1、设置当前控制器的标题

self.title = @"first";

    // navigationItem主要用来自定义控件的设置

self.navigationItem.title =@"First";

   // 2、设置视图背景颜色

self.view.backgroundColor =[UIColorredColor];

// iOS7.0之后,导航栏是透明的(外观的设置都是navigationBar)

// 当导航栏透明,视图从屏幕左上角开始;但如果不透明,视图从导航栏下面开始

   // 设置导航栏不透明 (navigationBar是导航控制器的独有属性,navigationItem是所有控制器都有的属性)

self.navigationController.navigationBar.translucent= NO;

   // 导航栏高度竖屏是44,横屏是32,状态栏竖屏20.

 

   // 3、设置导航栏的背景颜色

self.navigationController.navigationBar.barTintColor= [UIColorbrownColor];

 

   // 4.设置背景图片

   // 只有背景图片为44的时候,状态栏才不会被图片覆盖。

   [self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"NavBar_64.png"]forBarMetrics:UIBarMetricsDefault];

 

   // UINavigationBar主要对导航栏外观进行设置

   //  UINavigationItem主要进行自定义在导航栏上的控件

 

   // NavigationItem

    //1、定义左按钮

   // 三种自定义按钮

self.navigationItem.leftBarButtonItem  =[[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemEdittarget:selfaction:@selector(hehe)];

   // 防止被渲染:imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal

 

UIBarButtonItem *butItem =[[UIBarButtonItemalloc]initWithImage:[[UIImageimageNamed:@"NavBtnBack.png"]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlaintarget:self action:@selector(h)];

self.navigationItem.leftBarButtonItem =butItem;

 

UIBarButtonItem *butItem2  =[[UIBarButtonItemalloc]initWithTitle:@"Button"style:UIBarButtonItemStyleDonetarget:self action:@selector(next)];

//   self.navigationItem.leftBarButtonItem = butItem2

    ;

   // 设置多个右按钮

self.navigationItem.rightBarButtonItems =@[butItem,butItem2];

 

   // 设置标题视图

self.navigationItem.titleView =[[UISegmentedControlalloc]initWithItems:@[@"SB",@"NB"]];

    // 也可以用下面一种方法

//    UISegmentedControl *seg =[[UISegmentedControlalloc]initWithItems:@[@"SB",@"NB"]];

//   self.navigationItem.titleView = seg;

 

 

}

 

- (void)hehe{

self.view.backgroundColor =[UIColorbrownColor];

}

 

- (void)h{

self.view.backgroundColor =[UIColorgreenColor];

}

   //控制器的跳转

- (void)next{

SecondViewController *secVC =[[SecondViewControlleralloc]init];

   // 采用 push 往导航控制器中添加一个视图控制器

   // 导航控制器采用栈的形式管理着视图控制器

   [self.navigationControllerpushViewController:secVCanimated:YES];

}

 

- (void)didReceiveMemoryWarning {

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

 

 

@end

 

 

 

 

 

 

 

 

//

//  SecondViewController.m

 

#import "SecondViewController.h"

#import "RootViewController.h"

#import "ThirdViewController.h"

#

@interfaceSecondViewController ()

 

@end

 

@implementationSecondViewController

 

- (void)viewDidLoad {

   [superviewDidLoad];

self.view.backgroundColor =[UIColorwhiteColor];

 

self.navigationItem.leftBarButtonItem =[[UIBarButtonItemalloc]initWithTitle:@"back"style:UIBarButtonItemStylePlaintarget:self action:@selector(back)];

 

self.navigationItem.rightBarButtonItem =[[UIBarButtonItemalloc]initWithTitle:@"next"style:UIBarButtonItemStylePlaintarget:self action:@selector(next)];

 

   // 创建textField

self.textField =[[UITextFieldalloc]initWithFrame:CGRectMake(100, 100, 200, 30)];

self.textField.backgroundColor =[UIColorredColor];

 

   [self.viewaddSubview:self.textField];

 

}

   //左按钮方法

- (void)back{

 

   // 导航控制器里面的视图控制器

NSArray *arr =[self.navigationControllerchildViewControllers];

   // 当前正在展示的控制器就是栈定控制器

id VC =[self.navigationControllertopViewController];

id VC1 =[self.navigationControllervisibleViewController];

NSLog(@"%@ %@",VC,VC1);

   // 推出一个控制器(从栈里面推出一个控制器,回到根视图控制器)

   // 1、回到根视图控制器

   [self.navigationControllerpopToRootViewControllerAnimated:YES];

    // 2、回到上一个控制器

   [self.navigationControllerpopViewControllerAnimated:YES];

   // 3、回到指定控制器

    // 根据导航控制器栈里面的视图索引值进行 pop

   [self.navigationControllerpopToViewController:arr[0] animated:YES];

 

}

 

#pragma mark 属性传值

// 属性传值使用情况从前往后传,

// 必须拿到界面之间的联系点(因为此时可以同时访问到两个界面的对象)

  // 右按钮方法

- (void)next{

ThirdViewController *third =[[ThirdViewControlleralloc]init];

third.str = self.textField.text;

#pragma mark 第五步,设置代理

third.delegate = self;

   [self.navigationControllerpushViewController:thirdanimated:YES];

 

}

 

#pragma mark 第六步实现方法传值

- (void)passValue:(NSString *)TFText{

self.textField.text = TFText;

}

 

- (void)didReceiveMemoryWarning {

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

 

 

 

@end

 

 

 

 

 

 

 

 

//

// ThirdViewController.m

 

#import "ThirdViewController.h"

 

@interfaceThirdViewController ()

 

@end

 

@implementationThirdViewController

 

 

 

 

- (void)viewDidLoad {

   [superviewDidLoad];

self.view.backgroundColor =[UIColorwhiteColor];

 

self.lab =[[UITextFieldalloc]initWithFrame:CGRectMake(100, 100, 200, 30)];

self.lab.text = self.str;

NSLog(@"%@",self.str);

self.lab.backgroundColor =[UIColorgreenColor];

   [self.viewaddSubview:self.lab];

NSLog(@"%@",self.lab.text);

 

   // 自定义一个按钮返回上一个控制器

self.navigationItem.leftBarButtonItem =[[UIBarButtonItemalloc]initWithTitle:@"back"style:UIBarButtonItemStylePlaintarget:self action:@selector(back)];

 

}

 

#pragma mark 代理传值第三步

// 将值传递出去

- (void)back{

   [self.navigationControllerpopViewControllerAnimated:YES];

if (self.delegate != nil &&[self.delegaterespondsToSelector:@selector(passValue:)]) {

       [self.delegatepassValue:self.lab.text];

    }

}

 

 

 

- (void)didReceiveMemoryWarning {

   [superdidReceiveMemoryWarning];

   // Dispose of any resources that can be recreated.

}

 

 

 

@end

 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: