UINavigationController使用之手动添加UIBarButtonItem

原文地址;http://blog.csdn.net/totogo2010/article/details/7681879


1、UINavigationController导航控制器如何使用

UINavigationController可以翻译为导航控制器,在iOS里经常用到。

我们看看它的如何使用:

下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制器弹出堆栈。

上图来自苹果官网。


2、UINavigationController的结构组成

看下图,UINavigationController有Navigation bar  ,Navigation View ,Navigation toobar等组成。


现在我们建立一个例子,看看如何使用UINavigationController

3、新建一个项目

命名为UINavigationControllerDemo,为了更好理解UINavigationController,我们选择Empty Application模板


4、创建一个View Controller,命名为RootViewController:依次选择File——New——New File,默认勾上With XIB for user interface.


选择正确位置创建完成,这时项目里多了三个文件,分别是RootViewController.h RootViewController.m RootViewController.xib文件。

打开RootViewController.xib,添加一个按钮控件,按钮Button改成 :Goto SecondView,为跳转做准备


5、打开AppDelegate.h,向其中添加属性:

[cpp]  view plain copy
  1. @property (strong, nonatomic) UINavigationController *navController;  

添加后AppDelegate.h文件代码如下:

[cpp]  view plain copy
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @class ViewController;  
  4.   
  5. @interface AppDelegate : UIResponder <UIApplicationDelegate>  
  6.   
  7. @property (strong, nonatomic) UIWindow *window;  
  8.   
  9. @property (strong, nonatomic) ViewController *viewController;  
  10.   
  11. @property (strong, nonatomic) UINavigationController *navController;  
  12.   
  13. @end  

6、在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中创建添加navController,RootViewController视图。

[cpp]  view plain copy
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     RootViewController *rootView = [[RootViewController alloc] init];  
  5.     rootView.title = @"Root View";  
  6.       
  7.     self.navController = [[UINavigationController alloc] init];  
  8.     [self.navController pushViewController:rootView animated:YES];  
  9.     [self.window addSubview:self.navController.view];  
  10.     [self.window makeKeyAndVisible];  
  11.     return YES;  
  12. }  
给rootView的titie命名为 Root View,好识别View直接的切换关系。用pushViewController把rootView加入到navController的视图栈中。

7、现在Root视图添加完成

看看效果:

'

现在还没有Navigation bar 。只有title。

8、添加UIBarButtonItem

bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。

在RootViewController.m中添加代码如下:

[cpp]  view plain copy
  1. - (void)viewDidLoad  
  2. {  
  3.     [super viewDidLoad];  
  4.   
  5.     UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];  
  6.     self.navigationItem.leftBarButtonItem = leftButton;  
  7.       
  8.     UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd  target:self action:@selector(selectRightAction:)];  
  9.     self.navigationItem.rightBarButtonItem = rightButton;<p class="p1">}</p>  
这样添加了UIBarButtonItem了,效果如下:



这里重点介绍下

UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];

UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格,看下图,你不用一个个试验,你也知道想用那个item,如下图:


9、响应UIBarButtonItem的事件的实现

我们在 action:@selector(selectLeftAction:);

action添加了selectLeftAction和selectRightAction

在RootViewController.m文件中添加代码实现:

[cpp]  view plain copy
  1. -(void)selectLeftAction:(id)sender  
  2. {  
  3.     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  4.     [alter show];  
  5. }  
  6.   
  7. -(void)selectRightAction:(id)sender  
  8. {  
  9.     UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏右按钮" delegate:self  cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];  
  10.     [alter show];  
  11. }  
这样在点击左右的UIBarButtonItem时,弹出提示:



这篇先讲添加UIBarButtonItem,下篇讲解页面跳转和添加UISegmentedControl

下篇:iOS学习之UINavigationController详解与使用(二)页面切换和segmentedController


例子代码:https://github.com/schelling/YcDemo

著作权声明:上文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢

-----------------------------------------------------------
在上文的基础上,另一个博文作者对navigationcontroller如何手工添加navigationbar也提出了自己的看法。
原文地址:http://blog.csdn.net/nogodoss/article/details/21625267
 

UINavigationController的使用心得(一) 

分类: iphone   4194人阅读  评论(0)  收藏  举报

根据网上的一些资料,自己做了一个demo,先上效果


以上的效果是很丑的(只是表达一个意思)。

首先使用到了MainViewController,FirstViewController两个视图。

先创建FirstViewController视图。

 ///  MainViewController.m 

- (void)viewDidLoad

{

    [super viewDidLoad];

    //创建导航

    FirstViewController *viewController = [[FirstViewController allocinitWithNibName:nil bundle:nil];

    viewController.title =@"firstViewController";

    UINavigationController *navigationBar = [[UINavigationController allocinit];

    self.navigationController = navigationBar;

    [navigationBar pushViewController:viewController animated:YES];

    [self.view addSubview:navigationBar.view];

    [navigationBar release];

}

1.NavigationBar的使用

首先在 MainViewController.h文件中

@property(strong,nonatomic)UINavigationController *navigationController;

在MainViewController.m 中

//1.设置navigationBar的文本颜色

    self.navigationController.navigationBar.tintColor = [UIColor redColor];

//2.设置navigationBar的背景图片

    UIImage *navBarImage = [UIImage imageNamed:@"bottomBK.png"];

    UIImageView *imageView = [[UIImageView allocinitWithImage:navBarImage];

    [self.navigationController.navigationBar addSubview:imageView];

// 3.自定义 设置navigationBar的背景图片 的方式(采用类目的方式)

    参考 http://blog.csdn.net/zhuzhihai1988/article/details/7705308


在FirstViewController.m文件中

// 1.上面导航栏中的 左边按钮(系统)

    UIBarButtonItem *leftButton = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:nil action:@selector(leftButtonAction:)];

    self.navigationItem.leftBarButtonItem = leftButton;

    [leftButton release];

//2.上面导航栏中的  自定义左边按钮

    UIBarButtonItem *coutomButton = [[UIBarButtonItem allocinitWithTitle:@"asdfasdf"style:UIBarButtonItemStyleDone target:self action:@selector(rightButtonAction1:)];

    UIButton *button11 = [UIButton buttonWithType:UIButtonTypeCustom];

    [button11 setFrame:CGRectMake(004030)];

    [button11 setBackgroundColor:[UIColor redColor]];

    [button11 setBackgroundImage:[UIImage imageNamed:@"102.png"forState:UIControlStateNormal];

    [button11 setBackgroundImage:[UIImage imageNamed:@"102-sel.png"forState:UIControlStateHighlighted];

    coutomButton.customView = button11;

    [button11 release];

    self.navigationItem.leftBarButtonItem = coutomButton;

    [coutomButton release];

//3.自定义中间titleView

    UIButton *titleButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];

    titleButton.frame = CGRectMake(004030);

    [titleButton setBackgroundColor:[UIColor whiteColor]];

    [titleButton setTitle:@"titlView" forState:UIControlStateNormal];

    self.navigationItem.titleView = titleButton;

    [titleButton release];

// 4.上面导航栏中的右边按钮(系统)

  UIBarButtonItem *rightButton = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemCompose target:self action:@selector(rightButtonAction1:)];

    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:rightButton, nil];

    [rightButton release];

注意事项:

  Navigationbar 的位置坐标和宽度,高度是修改不了的。 即bar.frame = CGRectMake(0,20,self.view.bounds.size.width,30);重新设置navigationbar是不起作用的。


2.NavigationToolBar的使用

在MainViewController.m文件中

//1.显示底部工具栏

    self.navigationController.toolbarHidden = NO;

//2.设置底部工具栏的背景图片

    UIImage *navBarImage11 = [UIImage imageNamed:@"bottomBK.png"];

    UIImageView *imageView11 = [[UIImageView allocinitWithImage:navBarImage11];

    [self.navigationController.toolbar addSubview:imageView11];

//3.设置底部工具栏的文本颜色

    self.navigationController.toolbar.tintColor = [UIColor redColor];

//4.两个自定义tool下的按钮和系统save按钮

    UIBarButtonItem *custom = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemSearch target:nil action:nil];

    UIButton *button3 = [[UIButton allocinitWithFrame:CGRectMake(006044)];

    [button3 setBackgroundImage:[UIImage imageNamed:@"102.png"forState:UIControlStateNormal];

    custom.customView = button3;

    UIBarButtonItem *add = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:nil action:nil];

    UIButton *button4 = [[UIButton allocinitWithFrame:CGRectMake(006044)];

    [button4 setBackgroundImage:[UIImage imageNamed:@"102.png"forState:UIControlStateNormal];

    add.customView = button4;

    UIBarButtonItem *save = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemSavetarget:nil action:nil];

    UIBarButtonItem *save3 = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemSavetarget:nil action:nil];

    UIBarButtonItem *save1 = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemSavetarget:nil action:nil];

    UIBarButtonItem *save2 = [[UIBarButtonItem allocinitWithBarButtonSystemItem:UIBarButtonSystemItemSavetarget:nil action:nil];

    self.toolbarItems = [NSArray arrayWithObjects:custom,add,save,save1,save2,save3,nil];

    [button3 release];

    [button4 release];

    [custom release];

    [add release];

    [save release];

    [save1 release];

    [save2 release];

    [save3 release];

注意事项:

  NavigationToolbar 的位置坐标和宽度同NavigationBar的一样不能修改的。




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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值