19、UINavigationItem(UIBarButtonItem) - OC+UI

UINavigationItem

 

UINavigationItem 常用属性和方法:

UINavigationItem 初始化方法:

//
//  RootViewController.m
//  NavigetionBarDemo
//
//  Created by ccy on 13-12-29.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import "RootViewController.h"
#import "SecondViewController.h"

@interface RootViewController ()
{
    SecondViewController * _secondVC;
}

@end

@implementation RootViewController

- (void)dealloc
{
    [_secondVC release];
    [super dealloc];
}

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor greenColor];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    btn.frame = CGRectMake(10, 30, 300, 30);
    [btn setTitle:@"second view" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:btn];
    
    //标题
    //[self.navigationItem setTitle:@"root"];
    self.navigationItem.title = @"root";
    
    //标题视图 CGRectMake(100, 50, 100, 40);这个没有起作用,[视图居中显示]
    UIView *view = [[UIView alloc] init];
    view.frame = CGRectMake(100, 50, 100, 40);
    view.backgroundColor = [UIColor brownColor];
    self.navigationItem.titleView = view;
    [view release];
    
    /**************************************/
    //初始化导航条左按键标题
    //导航条风格
    self.navigationController.navigationBar.barStyle = UIBarStyleDefault;
    /*
     UIBarButtonItemStylePlain,   - 按键颜色和导航条颜色一样
     UIBarButtonItemStyleBordered,
     UIBarButtonItemStyleDone, - 按键颜色为蓝色
     */
    UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithTitle:@"左侧" style:UIBarButtonItemStyleBordered target:self action:@selector(itemClick:)];
    
    self.navigationItem.leftBarButtonItem = item1;
    [item1 release];
    
    /**************************************/
    //接收一个数组
    UIBarButtonItem * item2 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(itemClick:)];
   
    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeRoundedRect];
    // x,y 数值没用
    btn1.frame = CGRectMake(0 , 0, 50, 30);
    [btn1 setTitle:@"btn1" forState:UIControlStateNormal];
    [btn addTarget:self action:@selector(btnclick) forControlEvents:UIControlEventTouchUpInside];
    //初始化 接收一个按键
    UIBarButtonItem * item3 = [[UIBarButtonItem alloc] initWithCustomView:btn1];

    //添加一个图片控键[最适合的图片大小是40x40]
    UIBarButtonItem * item4 = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"itemImage.png"] style:UIBarButtonItemStylePlain target:self action:@selector(itemClick:)];
    
    NSArray * itemArray = [NSArray arrayWithObjects:item2,item3,item4, nil];
    [item2 release];
    [item3 release];
    
    //将 数组加入导航栏
    self.navigationItem.rightBarButtonItems = itemArray;
    
    _secondVC = [[SecondViewController alloc] init];
    
}

- (void)itemClick:(id)sender
{
    self.view.backgroundColor = [UIColor redColor];
}

- (void)btnclick
{
    [self.navigationController pushViewController:_secondVC animated:YES];
}

//
//  SecondViewController.m
//  NavigetionBarDemo
//
//  Created by ccy on 13-12-29.
//  Copyright (c) 2013年 ccy. All rights reserved.
//

#import "SecondViewController.h"

@interface SecondViewController ()

@end

@implementation SecondViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor redColor];
    [self.navigationItem setTitle:@"second"];
     /**************************************/
    //覆盖它的back按钮
    UIBarButtonItem * item1 = [[UIBarButtonItem alloc] initWithTitle:@"后退" style:UIBarButtonItemStyleBordered target:self action:@selector(itemClick:)];
    
    self.navigationItem.leftBarButtonItem = item1;
//    [item1 release];
    
}

- (void) itemClick:(id)sender
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值