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. Centos 查看内存

    1. 查看内存使用 free -mh 2. 读出的内核信息进行解释 cat /proc/meminfo MemTotal: kB #所有可用RAM大小 (即物理内存减去一些预留位和内核的二进制代码大小 ...

  2. 为什么32位系统最大支持4G内存??我自己悟出来了 终于 。。。。。

    今天突然开窍了,想通了..... 以下是我的抽象想法: 32位系统 这个 多少位 指的是 硬件的 一次性发送过来的位数,一个字节 等于8位,内存的一个存储单元就是一个字节,即8位. 也可以这样来想这个 ...

  3. 系统封装的dispatch系列代码块引起的循环引用

    整整一天的时间都在找内存泄漏,唯独遗漏了这个代码块,结果就是它,希望大家以后注意. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)( ...

  4. (30)C#Timer类

    有三种Timer 1.System.Windows.Forms.Timer 应用于WinForm中,它的主要缺点是计时不精确,而且必须有消息循环,Console  Application(控制台应用程 ...

  5. (26)C#WebService

    一.创建webservice 二.发布webservice 1.正式发布 (1)配置IIS 自己在局域网用的话,只需1,2,3 三步 1:网站的名称,将来IIS里有多个网站时可以方便区分 2:文件的本 ...

  6. 洛谷 P3804 后缀自动机

    题目描述 给定一个只包含小写字母的字符串SS , 请你求出 SS 的所有出现次数不为 11 的子串的出现次数乘上该子串长度的最大值. 输入输出格式 输入格式: 一行一个仅包含小写字母的字符串SS 输出 ...

  7. [HDU5528]Count a * b

    题目大意: 定义函数$f(m)=\displaystyle\sum_{a=0}^{m-1}\sum_{b=0}^{m-1}[m\nmid ab]$,$g(n)=\displaystyle\sum_{m ...

  8. hdu 1512 Monkey King 左偏树

    题目链接:HDU - 1512 Once in a forest, there lived N aggressive monkeys. At the beginning, they each does ...

  9. 2,搭建一个java开发环境

    (1)java开发需要的条件? 1)适用于环境开发的jdk(里面包括了jre和加热里面包括了jvm) 2)对应开发环境的eclipse 3)如果涉及到web开发,还需要web服务器(Tomcat) ( ...

  10. 转:大数据 2016 landscape

    如图: