2012.12.28
效果图:
[img]
[/img]
这次添加的导航条及上面的左右按钮,都是再代码中进行添加,并未使用xib文件进行界面设计。
一、新建一个Empty App工程:UINavigationControllerDemo
二、创建一个View Controller,命名为RootViewController:依次选择File——New——New File,默认勾上With XIB for user interface.
三、打开AppDelegate.h,向其中添加属性:
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@property (strong, nonatomic) UINavigationController *navController;
@end
四、在AppDelegate.m 文件的didFinishLaunchingWithOptions方法中创建添加navController,RootViewController视图。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootView = [[RootViewController alloc] init];
rootView.title = @"Root View";
self.navController = [[UINavigationController alloc] init];
[self.navController pushViewController:rootView animated:YES];
[self.window addSubview:self.navController.view];
[self.window makeKeyAndVisible];
return YES;
}
五、添加UIBarButtonItem
bar ButtonItem分左右UIBarButtonItem。我们把左右的都添加上去。
在RootViewController.m中添加代码如下:
- (void)viewDidLoad
{
[super viewDidLoad];
UIBarButtonItem *leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(selectLeftAction:)];
self.navigationItem.leftBarButtonItem = leftButton;
UIBarButtonItem *rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(selectRightAction:)];
self.navigationItem.rightBarButtonItem = rightButton;
六、响应UIBarButtonItem的事件的实现
我们在 action:@selector(selectLeftAction:);
action添加了selectLeftAction和selectRightAction
在RootViewController.m文件中添加代码实现:
-(void)selectLeftAction:(id)sender
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏左按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
-(void)selectRightAction:(id)sender
{
UIAlertView *alter = [[UIAlertView alloc] initWithTitle:@"提示" message:@"你点击了导航栏右按钮" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
[alter show];
}
这里重点介绍下
UIBarButtonItem *leftButton = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemActiontarget:selfaction:@selector(selectLeftAction:)];
UIBarButtonSystemItemAction的风格,这是系统自带的按钮风格,看下图,你不用一个个试验,你也知道想用那个item,如下图:
[img]
[/img]

- 大小: 103.2 KB

- 大小: 13 KB
分享到:
相关推荐
本篇文章将深入探讨`UINavigationController`的使用方法,特别是与ToolBar的结合。 首先,`UINavigationController`的核心功能是实现页面间的跳转和返回。当用户点击导航栏上的按钮或者执行某些操作时,它可以将新...
在iOS开发中,导航栏(UINavigationController)是应用界面中不可或缺的部分,通常包含左右两侧的UIBarButtonItem,用于展示图标或文字按钮。在某些情况下,我们可能需要自定义这些按钮的大小,使其更适合应用的设计...
#### 一、UIImageView 控件详解 **1.1 UIImageView 显示问题** 在 iOS 开发过程中,经常会遇到 UIImageView 的显示问题。默认情况下,若未设置 UIImageView 的尺寸,该控件将自动调整大小以适应所加载的图像尺寸。...
总的来说,iOS应用中使用Toolbar进行视图切换是一种常见的交互方式,通过理解并熟练掌握UIToolbar的使用,可以为用户提供更加直观和便捷的操作体验。记得在设计时兼顾用户体验和界面美观,使应用更加吸引人。
《iOS应用源码——WebsiteNavigation2详解》 在iOS开发领域,源码的学习与研究是提升技术能力的重要途径。本文将深入探讨名为“WebsiteNavigation2”的iOS应用源码,通过解析其核心架构、主要功能及实现机制,为iOS...
### iOS开发技巧详解 #### 1. TableView不显示没内容的Cell 在iOS开发中,TableView是非常常用的组件之一。有时我们需要让TableView不显示没有数据的Cell,以避免用户看到空白的Cell而产生困惑。实现这一功能的...
- 使用`UIBarButtonItem`可以创建导航栏上的按钮。你可以设置其为系统提供的图标,例如`initWithBarButtonSystemItem:`可以创建返回按钮。 以上就是关于iOS中UITableView和NavigationBar的一些常见设置和操作。在...