IOS 开发有关界面的东西不仅可以使用代码来编写,也可以使用Interface Builder可视化工具来编写。今天有个朋友问我这两个有什么区别,首先说说IB ,使用它编辑出来的控件其实底层还是调用代码只是苹果封装出来让开发者更好使用而已。它的优点是方便、快捷最重要的是安全,因为控件的释放它会帮我们完成不用手动释放。缺点是多人开发不好维护,就好比谁写的IB谁能看懂,别人看的话就比较费劲,不利于代码的维护。两种方式各有利弊,不过我个人还是比较喜欢纯代码,因为任何程序语言,或者任何脚本语言,代码和可视化工具比起来永远是最底层的。

利用代码在屏幕中添加一个标题栏,并且在标题栏左右两方在添加两个按钮,点击后响应这两个按钮。

这里设置标题栏的显示范围。

//创建一个导航栏
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(, , , )];

  有了标题栏后,须要在标题栏上添加一个集合Item用来放置 标题内容,按钮等

//创建一个导航栏集合
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil];

在这个集合Item中添加标题,按钮。
style:设置按钮的风格,一共有3中选择。
action:@selector:设置按钮点击事件。

//创建一个左边按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(clickLeftButton)]; //创建一个右边按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边"
style:UIBarButtonItemStyleDone
target:self
action:@selector(clickRightButton)];
//设置导航栏内容
[navigationItem setTitle:@"雨松MOMO程序世界"];

将标题栏中的内容全部添加到主视图当中。

//把导航栏添加到视图中
[self.view addSubview:navigationBar];

最后将控件在内存中释放掉,避免内存泄漏。

//释放对象
[navigationItem release];
[leftButton release];
[rightButton release];

添加这两个按钮的点击响应事件。

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

-(void)showDialog:(NSString *) str
{

UIAlertView * alert= [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil];

[alert show];
[alert release];
}

最后贴上完整的代码

#import "TitleViewController.h"

@implementation TitleViewController

- (void)didReceiveMemoryWarning
{
// Releases the view if it doesn't have a superview.
[super didReceiveMemoryWarning]; // Release any cached data, images, etc that aren't in use.
} #pragma mark - View lifecycle // Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
[super viewDidLoad]; //创建一个导航栏
UINavigationBar *navigationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(, , , )]; //创建一个导航栏集合
UINavigationItem *navigationItem = [[UINavigationItem alloc] initWithTitle:nil]; //创建一个左边按钮
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithTitle:@"左边"
style:UIBarButtonItemStyleBordered
target:self
action:@selector(clickLeftButton)]; //创建一个右边按钮
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithTitle:@"右边"
style:UIBarButtonItemStyleDone
target:self
action:@selector(clickRightButton)];
//设置导航栏内容
[navigationItem setTitle:@"雨松MOMO程序世界"]; //把导航栏集合添加入导航栏中,设置动画关闭
[navigationBar pushNavigationItem:navigationItem animated:NO]; //把左右两个按钮添加入导航栏集合中
[navigationItem setLeftBarButtonItem:leftButton];
[navigationItem setRightBarButtonItem:rightButton]; //把导航栏添加到视图中
[self.view addSubview:navigationBar]; //释放对象
[navigationItem release];
[leftButton release];
[rightButton release]; } -(void)clickLeftButton
{ [self showDialog:@"点击了导航栏左边按钮"]; } -(void)clickRightButton
{ [self showDialog:@"点击了导航栏右边按钮"]; } -(void)showDialog:(NSString *) str
{ UIAlertView * alert= [[UIAlertView alloc] initWithTitle:@"这是一个对话框" message:str delegate:self cancelButtonTitle:@"确定" otherButtonTitles: nil]; [alert show];
[alert release];
} - (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
} - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
// Return YES for supported orientations
return (interfaceOrientation == UIInterfaceOrientationPortrait);
} @end

来自:http://www.xuanyusong.com/archives/585

创建标题栏,UINavigationBar的使用的更多相关文章

  1. IOS 入门开发之创建标题栏UINavigationBar的使用(二)

    IOS 入门开发之创建标题栏UINavigationBar的使用 http://xys289187120.blog.51cto.com/3361352/685746     IOS 开发有关界面的东西 ...

  2. IOS 入门开发之创建标题栏UINavigationBar的使用

    转自:http://xys289187120.blog.51cto.com/3361352/685746 IOS 入门开发之创建标题栏UINavigationBar的使用     IOS 开发有关界面 ...

  3. 【Android】利用服务Service创建标题栏通知

    创建标题栏通知的核心代码 public void CreateInform() { //定义一个PendingIntent,当用户点击通知时,跳转到某个Activity(也可以发送广播等) Inten ...

  4. qml自定义标题栏

    要实现自定义的标题栏只需在原来的窗口的基础上创建一个Rectangle并将其定位在窗口顶部即可,实现代码如下: ApplicationWindow { id: mainWindow visible: ...

  5. Android学习之基础知识五—创建自定义控件

    下面是控件和布局的继承关系: 从上面我们看到: 1.所有控件都是直接或间接继承View,所有的布局都是直接或间接继承ViewGroup 2.View是Android中最基本的UI组件,各种组件其实就是 ...

  6. 自定义UINavigationBar的背景【转】

    from:http://cocoa.venj.me/blog/custom-navbar-background/ 为了让我们的应用程序更加美观,我们往往希望对iPhone自带的控件进行一点自定义.比如 ...

  7. Android一句代码给Activity定制标题栏

    在此之前,使用过几种方法设置标题栏: 1.常规法:这个方法是最常用的了,哪个activity需要什么样的标题栏,就在对应的xml布局设计.缺点:此方法维护起来困难,没有将标题栏的共性抽取出来, 如果要 ...

  8. poi创建excel文件

    package com.mozq.sb.file01.test; import org.apache.poi.hssf.usermodel.*; import org.apache.poi.hssf. ...

  9. 前端弹出对话框 js实现 ajax交互

    原本计划实现这样一个需求: 前台点击触发某业务动作,需要用户补充信息,不做跳转页面,弹窗的形式进行补充信息. 折腾出来了,但是最终没有用到. 代码还有些毛躁,提供大概实现逻辑. 实现思路: 在窗口铺上 ...

随机推荐

  1. kubernetes 搭建集群内服务

    nginx-rc.yaml apiVersion: v1 kind: ReplicationController metadata: name: webapp spec: replicas: 2 te ...

  2. Couchbase应用示例(初探)

    安装过程:略. 1. 新建Web项目 从NuGet获取并引用: CouchbaseNetClient,添加后引用列表显示为 : Couchbase.NetClient 2. 需要对项目添加引用,这里我 ...

  3. hdu 5159(概率)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5159 题解:假设在 x 张牌中选b张牌,那么有 x^b 种选法,如果在 (x-1) 张牌中选 b 张 ...

  4. react 利用react-hammerjs插件实现滑动特效和点击特效

    react-hammerjs是一款由hammer.js的JS插件来实现在react中实现手势滑动的事件插件, 它有各种各样的手势支持效果,这里我们就使用下它最简单的3种效果来实现我们要的动画 分别是点 ...

  5. 牛客练习赛16 A 字典序最大的子序列【贪心】

    链接:https://www.nowcoder.com/acm/contest/84/A 来源:牛客网 [出处]:http://codeforces.com/contest/196/problem/A ...

  6. bzoj 1305: [CQOI2009]dance跳舞

    题目链接 bzoj 1305: [CQOI2009]dance跳舞 题解 男,女生拆点A1A2,B1B2,拆成两点间分别连容量为K的边,限制与不喜欢的人跳舞的数量 A1连接源点容量为x,B1连接汇点容 ...

  7. 【BZOJ2276】Temperature

    题面 Description The Byteotian Institute of Meteorology (BIM) measures the air temperature daily. The ...

  8. adb devices 找不到设备怎么办 --- 2

    问题现象:在电脑上安装好手机驱动后,手机进入设置---->应用程序---->开发----->勾选USB调试后连接电脑,,在CMD命令中输入adb devices发现没有设备. 解决方 ...

  9. 【转】MySQL5.5的my.cnf 参数详解

    这篇文章很多地方只是翻译了my.cnf原始配置文件的说明 以下原文中有些参数事实上不适用于MySQL5.5,不知道原作者是否有经过实际测试,比如log-slow-queries应该写成slow-que ...

  10. UVa 407

    此问题与求上升序列最大和类似,可以作为DAG模型计算.将每一快砖分解为3块,将所有砖块按照底排序,注意sort排序中涉及到底的两个参数x,y,这时候一定要有优先排,比如先排x再排y,不能同时排x和y, ...