ios 导航页面


//  AppDelegate.m
#import "AppDelegate.h"
#import "ViewController.h"

@interface AppDelegate ()

@end

@implementation AppDelegate


- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    
    CGRect rect = [UIScreen mainScreen].bounds; //屏幕的大小
    UIWindow *w = [[UIWindow alloc]initWithFrame:rect];//新建一个页面  并且让新建的页面充满整个显示屏
    w.backgroundColor = [UIColor redColor]; //把新建的页面的颜色设为红色
    [w makeKeyAndVisible];//把设置的效果显示在页面上
    self.window = w;//使得开始设置的不要马上系统给销毁
    ViewController *v = [[ViewController alloc]init];//新建另一个页面
    UINavigationController *nav =[[UINavigationController alloc]initWithRootViewController:v];//初始画导航页面 并建立
    
    UIImage *image = [UIImage imageNamed:@"a"];//将要使用的图片赋值 并创建
    [nav.navigationBar setBackgroundImage:image forBarMetrics:UIBarMetricsDefault];//将图片添加到导航页面中
    
    
    nav.view.backgroundColor = [UIColor colorWithRed:0 green:1 blue:0 alpha:1];//给导航页面设置背景颜色 并用的是三元色来表示
    v.view.backgroundColor = [UIColor orangeColor];//将页面的背景颜色设置为橘红色
    self.window.rootViewController = nav;//把创建好的导航页面在第一个页面上显示
   
    return YES;
}


//  ViewController.m

#import "ViewController.h"
#import "SecondViewController.h"

@interface ViewController ()

@end

@implementation ViewController

//视图加载完成后自动调用
- (void)viewDidLoad {
    [super viewDidLoad];
    
//    self.title = @"AAA";   //第一种方法 在导航页面显示标签
    self.navigationItem.title = @"AAA";//第二种方法 在导航页面显示标签
    
    //1.
    UIBarButtonItem *leftItem = [[UIBarButtonItem alloc] initWithTitle:@"Left" style:UIBarButtonItemStylePlain target:self action:@selector(didLeftClicked)];//
    
    //2.
    UIImage *img = [UIImage imageNamed:@"back"];//添加图片 并初始化
//    UIBarButtonItem *leftItem2 = [[UIBarButtonItem alloc] initWithImage:img style:UIBarButtonItemStyleDone target:self action:@selector(didLeftClicked)];//
    
    //3. 使用一个自定义的控件作为左边item
//    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];//
    UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];//创建一个按钮
    [leftBtn setImage:img forState:UIControlStateNormal];//将图片加载在按钮上
    //只有大小有作用
    leftBtn.frame = CGRectMake(0, 0, 40, 40);//按钮的位置大小
    
    UIBarButtonItem *leftItem3 = [[UIBarButtonItem alloc] initWithCustomView:leftBtn];//
    //设置导航条左/右侧按钮
    self.navigationItem.rightBarButtonItems = @[leftItem3];//
    
    UIImageView *imgView = [[UIImageView alloc] initWithImage:img];//
    self.navigationItem.titleView = imgView;//
    
    //导航条分为3个部分
    //0. self.navigationController.navigationBar
    //1. self.navigationItem.leftBarButtonItem(s)
    //2. self.navigationItem.rightBarButtonItem(s)
    //3. self.navigationItem.title(View)
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];//创建一个按钮
    //设置不同状态的标题
    [btn setTitle:@"按钮" forState:UIControlStateNormal];//在常态的时候按钮显示为 “按钮”
    [btn setTitle:@"高亮" forState:UIControlStateHighlighted];//在点击按钮时 按钮显示为“高亮”
//    [btn setBackgroundColor:[UIColor redColor]];//第一种方法表示按钮的背景颜色为红色
    btn.backgroundColor = [UIColor redColor];//第二种方法表示 按钮的背景颜色为红色
    
    [btn addTarget:self action:@selector(didClicked) forControlEvents:UIControlEventTouchUpInside];//点击按钮将要执行的方法名还有执行的类型
    btn.frame = CGRectMake(0, 64, 100, 100);//设置位置和大小
    [self.view addSubview:btn];//显示按钮
}

- (void)didLeftClicked//按钮执行的方法
{
    NSLog(@"%s", __PRETTY_FUNCTION__);
}

- (void)didClicked//按钮执行的方法
{
    NSLog(@"%s", __func__);
    
    SecondViewController *viewCtrl = [[SecondViewController alloc] init];//
//    viewCtrl.view.backgroundColor = [UIColor orangeColor];//
//    [self presentViewController:viewCtrl animated:YES completion:nil];//NULL
    
    [self.navigationController pushViewController:viewCtrl animated:YES];//
    //所有被导航控制器管理的页面,都会拥有一个指针navigationController指向所在的导航控制器
    NSLog(@"2: %p", self.navigationController);//
}

@end

 

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"Second";//在第二个页面中的导航页面标题设置为这个
    
    self.view.backgroundColor = [UIColor purpleColor];//设置页面的背景颜色
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];//设置一个按钮
    [btn setTitle:@"第二个按钮" forState:UIControlStateNormal];//设置一个常规按钮 并命名为第二个按钮
    [btn addTarget:self action:@selector(didClicked:) forControlEvents:UIControlEventTouchUpInside];//这个按钮将执行自己的didclicked方法
    btn.frame = CGRectMake(100, 100, 200, 50);//设置按钮所在页面的位置
    [self.view addSubview:btn];//把设置的按钮加载在页面上
    
    NSLog(@"btn 1: %p", btn);
}

- (void)didClicked:(UIButton *)sender   //按钮需要执行的方法
{
    NSLog(@"%s", __func__);
    NSLog(@"btn 2: %p", sender);
    
    [self.navigationController popViewControllerAnimated:YES];//将要跳转到前一个页面
}

 
posted @ 2015-01-05 19:17  代佳宏  阅读(216)  评论(0编辑  收藏  举报