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. WIN32窗口程序

    // Win32.cpp : 定义应用程序的入口点. // #include "stdafx.h" #include "Win32.h" void TRACE( ...

  2. Visual Studio跨平台开发(1):Hello Xamarin!

    前言 应用程序发展的脚步, 从来没有停过. 从早期的Windows 应用程序, 到网络时代的web 应用程序, 再到近几年相当盛行的行动装置应用程序(Mobile Application), 身为C# ...

  3. JavaScript 性能优化的小知识总结

    前言 一直在学习 javascript,也有看过<犀利开发 Jquery 内核详解与实践>,对这本书的评价只有两个字犀利,可能是对 javascript 理解的还不够透彻异或是自己太笨,更 ...

  4. UVA 437 巴比伦塔 【DAG上DP/LIS变形】

    [链接]:https://cn.vjudge.net/problem/UVA-437 [题意]:给你n个立方体,让你以长宽为底,一个个搭起来(下面的立方体的长和宽必须大于上面的长和宽)求能得到的最长高 ...

  5. Codeforces Round #277.5 (Div. 2) B. BerSU Ball【贪心/双指针/每两个跳舞的人可以配对,并且他们两个的绝对值只差小于等于1,求最多匹配多少对】

    B. BerSU Ball time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  6. 洛谷 P2183 [国家集训队]礼物

    题目描述 一年一度的圣诞节快要来到了.每年的圣诞节小E都会收到许多礼物,当然他也会送出许多礼物.不同的人物在小E心目中的重要性不同,在小E心中分量越重的人,收到的礼物会越多.小E从商店中购买了n件礼物 ...

  7. 当ASP.NET Forms验证方式遭遇苹果IOS

    一.问题出现 我在用ASP.NET MVC4做微信开发的时候,用Forms验证方式做为authentication. 一般都是在web.config加: <authentication mode ...

  8. jqgrid postData setGridParam 调用多次时查询条件累加的问题--详情页查询导致的无法在新的页面中查询

    $("#btn_search").click(function () { url = "/AMEvents/GetGridJson?evtype=1"; var ...

  9. iptables 要点总结

    http://jiayu0x.com/2014/12/02/iptables-essential-summary/

  10. How to: Launch the Debugger Automatically

    Sometimes, you may need to debug the startup code for an application that is launched by another pro ...