//
// NavigationDemoViewController.m
// AppUI组件学习
//
// Created by 麦子 on 15/6/20.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "NavigationDemoRootViewController.h"
#import "NavigationDemoOneViewController.h"
@interface NavigationDemoRootViewController ()
@end
/*
NavigationViewController包括:
UI: navigationBar,toolbar ,和中间显示的部分。 其中这两个bar都是用UIBarButtonItem这个来进行填充,数组和单个都可以,同时他还限制了左边和右边这样的形式。
他时以栈的形式来进行管理,弹出或者是押入,界面显示的总是栈区的第一个对象界面。跳转的时候,可以指定跳转到入栈的其中一个对象中去。
传值: 正面传值,相当于Android中Intent中直接放入了一个对象,然后在获取的类中以属性获取。
返向传值: 通过回调函数的形式。 在实现类中设置一个接口为属性。 在触发的类中,以这个协议调用方法。在IOS中,代理就是一个回调而已。 协议就是接口。
**/
@implementation NavigationDemoRootViewController{
UIButton *but;
}
- (id)init{
id obj = [super init];
if (obj != nil) {
self.hidesBottomBarWhenPushed = true;
}
return obj;
}
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor orangeColor];
[self createView];
[self navigationSet];
[self navigationItemSet];
}
// 导航栏常用属性
- (void)navigationSet{
// 设置导航栏的样式
self.navigationController.navigationBar.barStyle = UIBarStyleBlackTranslucent;
// self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
// 没什么效果
// self.navigationController.navigationBar.backgroundColor = [UIColor redColor];
// 竖屏:人像模式 , 横评:风景模式, 如果图片过大,自己会裁减大小
// [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"tupian2.jpg"] forBarMetrics:UIBarMetricsDefault];
// 设置裁减
// self.navigationController.navigationBar.clipsToBounds = YES;
// 隐藏导航条, 隐藏后,里面的组件会向上移动,自动扩从空间。
// self.navigationController.navigationBar.hidden = YES;
}
// navigationItems 属性
- (void)navigationItemSet{
[self.navigationItem setTitle:@"root"];
//设置titleView , 其中的视图大小不受控制
// UIView *view = [[UIView alloc] init];
// view.frame = CGRectMake(100, 100, 100, 100);
// view.backgroundColor = [UIColor yellowColor];
// [self.navigationItem setTitleView:view];
// 设置左侧按钮
UIBarButtonItem *leftBut = [[UIBarButtonItem alloc] initWithTitle:@"上一级" style:UIBarButtonItemStylePlain target:self action:@selector(butClick:)];
self.navigationItem.leftBarButtonItem = leftBut;
UIBarButtonItem *rightBut = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(butClick:)];
UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
btn.frame = CGRectMake(0, 0, 40, 30);
btn.backgroundColor = [UIColor redColor];
UIBarButtonItem *rightBut2 = [[UIBarButtonItem alloc] initWithCustomView:btn];
// 有右向左走的
NSArray *array = [NSArray arrayWithObjects:rightBut,rightBut2, nil];
self.navigationItem.rightBarButtonItems = array;
// toolBar 设置
self.navigationController.toolbarHidden = NO;
// 图片不会自动裁减
[self.navigationController.toolbar setBackgroundImage:[UIImage imageNamed:@"tupian3.jpg"] forToolbarPosition:UIBarPositionAny barMetrics:UIBarMetricsDefault];
// UIToolbar *bar = [[UIToolbar alloc] init];
UIBarButtonItem *toolBtn1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(butClick:)];
UIBarButtonItem *toolBtn2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(butClick:)];
// 这个会自动算出来的,会自动排版
UIBarButtonItem *toolBtn3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:self action:@selector(butClick:)];
NSArray *array2 = [NSArray arrayWithObjects:toolBtn1,toolBtn3,toolBtn2, nil];
self.toolbarItems = array2;
}
- (void)butClick:(UIBarButtonItem *)but{
// [self.navigationItem setTitle:@"点击左侧按钮"];//这个值会在下一级显示出效果
// self.view.backgroundColor = [UIColor blackColor];
}
- (void)createView{
but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame = CGRectMake(20, 80, 100, 50);
[but setTitle:@"进栈" forState:UIControlStateNormal];
but.backgroundColor = [UIColor yellowColor];
[but addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
}
- (void)btnClick{
// 加入自定义动画
[self.navigationController.view.layer addAnimation:[NavigationDemoRootViewController addTransaction] forKey:nil];
NavigationDemoOneViewController *one = [[NavigationDemoOneViewController alloc]init];
// 正向传值 相当于传了一个对象过去。 ---数据都通过对象属性来进行传递
one.titleName = @"titleName--Test";
one.bussinessDelegate = self;
[self.navigationController pushViewController:one animated:true];
// 反向传值---- 第二个界面向第一个对象传递。
}
- (void)messageSet:(id) obj{
UIButton *btn = (UIButton *)obj;
NSString *str = [btn titleLabel].text;
NSLog(@"%@",str);
self.navigationItem.title =str;
[but setTitle:str forState:UIControlStateNormal];
}
+ (CATransition *)addTransaction{
CATransition *animation=[CATransition animation];
//设置动画效果
[animation setType:@"rippleEffect"];
//
// pageCurl 向上翻一页
// pageUnCurl 向下翻一页
// rippleEffect 滴水效果
// suckEffect 收缩效果,如一块布被抽走
// cube 立方体效果
// oglFlip 上下翻转效果
//设置动画方向
[animation setSubtype:kCATransitionFromBottom];
//设置动画播放时间
[animation setDuration:1.0f];
//设置动画作用范围
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];
return animation;
}
@end
//
// navigationDemoOneViewController.m
// AppUI组件学习
//
// Created by 麦子 on 15/6/20.
// Copyright (c) 2015年 麦子. All rights reserved.
//
#import "NavigationDemoOneViewController.h"
#import "NavigationDemoRootViewController.h"
#import "NavigationDemoTwoViewController.h"
@interface NavigationDemoOneViewController ()
@end
@implementation NavigationDemoOneViewController
@synthesize bussinessDelegate;
@synthesize titleName;
- (void)viewDidLoad {
[super viewDidLoad];
self.view.backgroundColor = [UIColor purpleColor];
[self createView];
self.navigationController.navigationBar.hidden = NO;
[self navigationItemSet];
}
// navigationItems 属性
- (void)navigationItemSet{
[self.navigationItem setTitle:self.titleName];
// 定制每一个ViewController中的LeftButton
UIBarButtonItem *leftBtn = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(btnClick:)];
self.navigationItem.leftBarButtonItem = leftBtn;
}
- (void)createView{
UIButton *but = [UIButton buttonWithType:UIButtonTypeRoundedRect];
but.frame = CGRectMake(20, 80, 100, 50);
but.tag = 1;
[but setTitle:@"出栈" forState:UIControlStateNormal];
but.backgroundColor = [UIColor yellowColor];
[but addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
UIButton *butC = [UIButton buttonWithType:UIButtonTypeRoundedRect];
butC.frame = CGRectMake(20, 320, 100, 50);
butC.tag = 4;
[butC setTitle:@"进栈跳转" forState:UIControlStateNormal];
butC.backgroundColor = [UIColor yellowColor];
[butC addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:but];
[self.view addSubview:butC];
}
- (void)btnClick:(UIButton *)but{
if ([but isKindOfClass:[UIButton class]]) {
switch (but.tag) {
case 1:{
[self.navigationController.view.layer addAnimation:[NavigationDemoRootViewController addTransaction] forKey:nil];
[self.navigationController popViewControllerAnimated:true];
// 调用接口
if ([bussinessDelegate respondsToSelector:@selector(messageSet:)]) {
[bussinessDelegate messageSet:but];
}
break;
}
case 4:{
NavigationDemoTwoViewController *two = [[NavigationDemoTwoViewController alloc] init];
[self.navigationController pushViewController:two animated:true];
break;
}
default:
break;
}
}
}
@end