导航栏(UINavigationBar、UINavigationItem)

AppDelegate.m:

#import "AppDelegate.h"
#import "RootViewController.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    
    RootViewController *rootViewController = [[RootViewController alloc] init];
    self.window.rootViewController = rootViewController;
    
    return YES;
}


RootViewController.m:

#import "RootViewController.h"

@interface RootViewController ()

@end


@implementation RootViewController


- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        self.title = @"导航";
    }
    return self;
}


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    //创建一个导航栏
    UINavigationBar *navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, 320, 44+20)];
    
    //创建一个导航栏集合
    UINavigationItem *navItem = [[UINavigationItem alloc] initWithTitle:nil];
    
    
    //在这个集合Item中添加标题,按钮
    //style:设置按钮的风格,一共有三种选择
    //action:@selector:设置按钮的点击事件
    //创建一个左边按钮
    UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边" style:UIBarButtonItemStylePlain target:self action:@selector(clickLeftButton)];
    //创建一个右边按钮
    UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边" style:UIBarButtonItemStylePlain target:self action:@selector(clickRightButton)];
    
    //设置导航栏的内容
    [navItem setTitle:@"导航栏"];
    
    //把导航栏集合添加到导航栏中,设置动画关闭
    [navBar pushNavigationItem:navItem animated:YES];
    
    // 把左右两个按钮添加到导航栏集合中去
    [navItem setLeftBarButtonItem:leftButton];
    [navItem setRightBarButtonItem:rightButton];
    
    // 将导航栏中的内容全部添加到主视图当中
    [self.view addSubview:navBar];
    
    // 最后将控件在内存中释放掉,以避免内存泄露
    // [navItem release];
    // [leftButton release];
    // [rightButton release];
}


-(void) clickLeftButton
{
    [self showDialog:@"点击了导航栏左边按钮"];
}


-(void) clickRightButton
{
    [self showDialog:@"点击了导航栏右边按钮"];
}


-(void)showDialog:(NSString *)str
{
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"title" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil];
    [alert show];
    // [alert release];
}


代码例子下载:http://pan.baidu.com/s/1o6qc9Yi





你可能感兴趣的:(导航栏(UINavigationBar、UINavigationItem))