UI21导航控制器

代码列表

160814_vZZJ_3228897.png

效果图:

160951_yjau_3228897.png160930_j6hm_3228897.png

AppDelegate.m

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

    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColor whiteColor];

    [self.window makeKeyAndVisible];

    RootViewController *root = [[RootViewController alloc] init];

    //创建导航器

    UINavigationController * nac = [[UINavigationController alloc] initWithRootViewController:root];

    self.window.rootViewController=nac;

    //导航器颜色

    nac.navigationBar.backgroundColor = [UIColor yellowColor];

    //半透明

    nac.navigationBar.translucent = NO;

    

    

    return YES;

}

 

 

RootViewController.m

#import "RootViewController.h"

#import "SecondViewController.h"

 

@interface RootViewController ()<chuanZhiDalegate>

{

    UITextField *tf;

}

@end

@implementation RootViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

    

    UIView * view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];

    view.backgroundColor = [UIColor redColor];

    [self.view addSubview:view];

    

    //把控制导航栏内容的代码写到对应的页面中来

    self.navigationItem.title = @"首页";

//    self.navigationItem.titleView =

    

    //左右侧的按钮

//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"右侧" style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)];

    //图片右侧按钮

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithImage:[UIImage imageNamed:@"settings" ]  style:UIBarButtonItemStylePlain target:self action:@selector(touchRight)];

    //属性传值代理传值

    // 第一个页面中的输出框

    tf = [[UITextField alloc] initWithFrame:CGRectMake(0, 150, 150, 40)];

    tf.backgroundColor = [UIColor redColor];

    [self.view addSubview:tf];    

}

-(void) touchRight {

    NSLog(@"点了右侧");

    SecondViewController * secondVC= [[SecondViewController alloc] init];

    //跳转到第二页面

    [self.navigationController pushViewController:secondVC animated:YES];

    

    //传值

    secondVC.inputValue = tf.text;

    secondVC.delegate = self;

}

//代理传回的值

- (void)chuanZhi:(NSString *)str {

    NSLog(@"代理传回的值:%@",str);

}

 

SecondViewController.m

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.

     self.view.backgroundColor = [UIColor yellowColor]; 

    //第二个页面左上角的返回按钮

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(touchLeftBack)]; 

    //第二个页面中的Label

    UILabel * label = [[UILabel alloc] initWithFrame:CGRectMake(100, 100, 150, 40)]; 

    label.text = self.inputValue;

    label.enabled = YES;

    [self.view addSubview:label]; 

    // 代理传值 

}

 

-(void) touchLeftBack {

    NSLog(@"左侧返回按钮");

    //返回时触发代理方法 将值传递回去

    [self.delegate chuanZhi:@"这个是返回的值"];

    

    // 返回上级视图

    [self.navigationController popToRootViewControllerAnimated:YES];

    // 返回到根视图

    //[self.navigationController popToRootViewControllerAnimated:YES];

    //指定到某个页面 少用到

    //    [self.navigationController popToViewController:<#(nonnull UIViewController *)#> animated:<#(BOOL)#>

}

 

 

转载于:https://my.oschina.net/VincentOSC/blog/830298

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值