iOS开发 — (UINaVigationController)导航控制器,界面传值

UINavigationController 继承于 UIViewController , 以栈的方式管理所 控制的视图控制器 。
至少要有一个被管理的视图控制器 ,这个控制器我们称作导航控制器的根视图控制器 。
不论什么继承自 UIViewController 的类(多态)都能够作为根控制器。
一: 工作原理:
UINavigationController 通过 栈 的⽅式管理控制器的切换, 控制⼊栈和出栈 来展⽰示各个视图控制器 。
UINavigationController 的 ContentView ⾥里始终显⽰栈顶 控制器的 view 。
viewControllers 属性存储了栈中的全部被管理的控制器 navigationController 属性 ,父类中的属性, 每一个在栈中的控制器 ,都能通过此属性,获取⾃己所在的 UINavigationController 对象

经常使用属性

viewControllers   // 全部处于栈中的控制器
topViewController   // 位于栈顶的控制器
visibleViewController   // 当前正在显⽰示的控制器
navigationBar   // 导航条

navigationBar 是导航条。 iOS7 之后默认是透明的, iOS7 之前默认是不透明 的。


navigationBar 在透明情况,与 contentView 会重合⼀部分区域。


navigationBar 在不透明情况, contentView 跟在 navigationBar 的下⾯。
navigationBar 竖屏下默认⾼高度 44 ,横屏下默认⾼度 32

这里写图片描写叙述

二:<代码展示>
1:设置根视图控制器

"AppDelegate.m"
//1:创建firstVC
    FirstViewController *firstVC = [[FirstViewController alloc]init];
//2.创建导航视图控制器对象  naVC
    UINavigationController *naVC = [[UINavigationController alloc]initWithRootViewController:firstVC];
//3.将导航视图控制器对象 naVC 指定为window根视图控制器
    self.window.rootViewController = naVC;
//4.释放全部权
    [naVC release];
    [firstVC release];

在这个文件中能够定制 navigationBar

//1.设置barTincolor
   // naVC.navigationBar.barTintColor = [UIColor cyanColor];
    //关闭半透明
    naVC.navigationBar.translucent =  NO; 
//2.设置tinColor
 //   naVC.navigationBar.tintColor = [UIColor yellowColor];
//3.设置风格  默认是0 白色  1是黑色
    naVC.navigationBar.barStyle = 1;

//4.设置背景图片
//当我们navigation设置图片的时候 navigation不再透明
//图片尺寸:
//当小于44或者大于64时候 图片会在navigationBar和satusBar上产生平铺的效果
//当尺寸正好等于44时候 图片仅仅会位navigationBar附上图片
//当尺寸正好等于64时候,图片会为navigationBar以及statusBar同一时候附上图片;

 [naVC.navigationBar setBackgroundImage:[UIImage imageNamed:@"32074.png"] forBarMetrics:UIBarMetricsDefault];

//隐藏navigationBar(导航栏)和StatusBar(状态栏)
naVC.navigationBarHidden = NO;

在根视图控制器
1:设置导航栏标题

self.navigationItem.title = @"first";

或者

 self.title = @"First(主页)";

2:设置右边的Barbutton
//方法一 使用的系统的图标

   self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:self action:@selector(nextAction:)];

//方法二 通过View自己定义创建

  //创建button
    UIButton *BT = [UIButton buttonWithType:UIButtonTypeSystem];
    [BT setTitle:@"下一页" forState:UIControlStateNormal];
    [BT addTarget:self action:@selector(nextAction:) forControlEvents:UIControlEventTouchUpInside];
    BT.frame = CGRectMake(0, 0, 60, 50);
    //通过button 创建barButtonItem
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithCustomView:BT];
    //给rightBatButtonItem赋值
    self.navigationItem.rightBarButtonItem = rightItem;
    //释放全部权
    [rightItem release];

3:设置左边的返回按钮 一般使用系统的图标

self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(LastAction:)];
pushViewController:animated     // 进⼊入下⼀个视图控制器
popViewControllerAnimated :       // 返回上⼀个视图控制器
popToViewController:animated   // 返回到指定的视图控制器
popToRootViewControllerAnimated    // 返回到根视图控制器

4:触发事件 从根视图推到下一视图push

-(void)nextAction:(UIBarButtonItem *)Bt
{
    //创建下一个视图控制器对象
    SecondViewController *secondVC = [[SecondViewController alloc]init];
    //2. 通过导航视图控制器 push操作
    [self.navigationController pushViewController:secondVC animated:YES];
    [secondVC release];
}

5:从下一视图返回根视图pop

-(void)LastAction:(UIBarButtonItem *)BT{
    [self.navigationController popViewControllerAnimated:YES];
}

注:UIBarButtonItem 属于 MVC 的 M 。定义了 UINavigationItem 上按钮的触 发事件。外观等

initWithBarButtonSystemItem:target:action: 
-initWithTitle:style:target:action: 
-initWithImage:style:target:action: 

界面传值
场景1 : 从前往后传值 First(根视图) –> Second(第二个视图)
方法: 属性传值
步骤:1.在Second中声明对于类型的属性

@property (nonatomic,copy)NSString *tfStr; //用来接收 字符串

2.在First中push的方法中 将值传给Second的属性

secondVC.tfStr = _tf.text;

3.在Second中 使用属性进行操作

_tf.text = _tfStr;    //接收来自第一个视图的textFiled的值

场景2 : 从后往前传值 Second(第二个视图) –> First(根视图)
方法 :代理传值
步骤: 1.在Second中声明协议 声明代理

@protocol SecondViewControllerDelegate <NSObject>

- (void)secondViewControllerDidDisAppearWithString:(NSString *)string; // 通知 第一个界面 运行方法 将字符串拿走
@end
@property (nonatomic,assign)id<SecondViewControllerDelegate>delegate; //声明协议的代理

2.在Second将要消失的时候 通知代理运行协议中的方法 将相应的值拿走

-(void)viewDidDisappear:(BOOL)animated{
    [super viewDidDisappear:animated];
    //通知代理运行协议中的方法 把tf上的字符串拿走
    [_delegate secondViewControllerDidDisAppearWithString:_tf.text];
}

3.在First中 找到Second对象 并把First设置为Second
这里写代码片….

secondVC.delegate = self;  //设置代理

4.在First中 实现协议中的方法 获取相应的值
实现协议中的方法

-(void)secondViewControllerDidDisAppearWithString:(NSString *)string{
//通过传过来的參数 string 给textFiled 赋值
    _tf.text = string;
}
原文地址:https://www.cnblogs.com/yjbjingcha/p/7363057.html