NatvigationController Demo

网上很多人写的都很烦,要么太理论,要么不好实现.写个demo帮助理解

//AppDelegate.m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    
    self.window.backgroundColor=[UIColor whiteColor];
    
    RootViewController *rootView=[[RootViewController alloc]init];
    
    UINavigationController *nav = [[UINavigationController alloc]initWithRootViewController:rootView];
    
    [_window setRootViewController:nav];
    
   
    
    return YES;
}

我的RootView.ViewController

//
//  ViewController.m
//  First
//
//  Created by zcw on 2017/12/6.
//  Copyright © 2017年 zcw. All rights reserved.
//

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
{
    double length;   // Length of a box
    double breadth;  // Breadth of a box
    double height;   // Height of a box
}
@end

@implementation ViewController
@synthesize mystr = _myStr;
- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    //Nav
    [self.navigationController.navigationBar setTranslucent:NO];//设置navigationbar的半透明

    self.title = @"RootViewController";//设置navigationbar上显示的标题

    [self.navigationController.navigationBar setBarTintColor:[UIColor purpleColor]];//设置navigationbar的颜色

    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStyleDone target:self action:Nil];//设置navigationbar左边按钮

    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonItemStylePlain target:self action:Nil];//设置navigationbar右边按钮

    [self.navigationController.navigationBar setTintColor:[UIColor whiteColor]];//设置navigationbar上左右按钮字体颜色
    
//    self.navigationItem.rightBarButtonItem setTarget:<#(id)#>
//    动态添加一个按钮
    
    
    
    // Do any additional setup after loading the view, typically from a nib.
    UIButton * button = [[UIButton alloc] init];
    
    [button setTitle:@"点击我跳转" forState:UIControlStateNormal];
    [button setTitle:@"摸我干啥" forState:UIControlStateHighlighted];
    
    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    
    UIImage *imgNormal = [UIImage imageNamed:@"btn_01"];
    UIImage *imgHighted = [UIImage imageNamed:@"btn_02"];
    
    [button setBackgroundImage:imgNormal forState:(UIControlStateNormal)];
    [button setBackgroundImage:imgHighted forState:(UIControlStateHighlighted)];
    button.frame = CGRectMake(50, 50, 299, 126);
    [button addTarget:self action:@selector(buttonPrint) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:button];
    
    
    
    
}

- (void)buttonPrint
{
    printf("测试打印");
    SecondViewController *nextVc = [[SecondViewController alloc]init];
//    [self presentViewController:nextVc animated:YES completion:nil];
    
    [self.navigationController pushViewController:nextVc animated:YES];
    


   
    

  
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

}


@end

SecondVIewController

//
//  SecondViewController.m
//  First
//
//  Created by zcw on 2017/12/6.
//  Copyright © 2017年 zcw. All rights reserved.
//

#import "SecondViewController.h"
#import "ViewController.h"
@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    UILabel *label = [[UILabel alloc] init];
    label.text = @"second page";
    [label setTextColor:[UIColor yellowColor]];
    label.frame = CGRectMake(200, 200, 50, 200);
    
    
    
    UIButton * button = [[UIButton alloc] init];
    
    [button setTitle:@"-----点击我跳转" forState:UIControlStateNormal];
    [button setTitle:@"-----摸我干啥" forState:UIControlStateHighlighted];
    
    [button setTitleColor:[UIColor yellowColor] forState:UIControlStateNormal];
    [button setTitleColor:[UIColor greenColor] forState:UIControlStateHighlighted];
    
    UIImage *imgNormal = [UIImage imageNamed:@"btn_01"];
    UIImage *imgHighted = [UIImage imageNamed:@"btn_02"];
    
    [button setBackgroundImage:imgNormal forState:(UIControlStateNormal)];
    [button setBackgroundImage:imgHighted forState:(UIControlStateHighlighted)];
    button.frame = CGRectMake(50, 50, 299, 126);
    [button addTarget:self action:@selector(buttonPrint) forControlEvents:(UIControlEventTouchUpInside)];
    
    [self.view addSubview:button];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)buttonPrint
{
    printf("测试打印2222");
//    [self dismissViewControllerAnimated:true completion:nil];
    [self.navigationController popToRootViewControllerAnimated:YES];//回到根视图界面
}


@end

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
禁止转载,如需转载请通过简信或评论联系作者。

推荐阅读更多精彩内容

  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 14,205评论 4 61
  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 175,378评论 25 709
  • 春天到了,万物复苏,绿意盎然,到处都是生机勃勃的景象。 一间中学门口的木棉树花开了,听长辈说到一句民间俗语:木棉花...
    岭南后生阅读 1,313评论 0 2
  • 1、检查系统环境 2、创建第一个tabs 由于国内防火墙限制,安装过程中可能会有如下报错 3、根据提示重新修复安装...
    魔都老王阅读 5,126评论 0 1
  • 三个臭皮匠顶个诸葛亮
    梵音瑜伽谢霆锋阅读 890评论 0 0