UINavigationController又被成为导航控制器,继承自UIViewController,以栈的方式管理所控制的视图控制器,下面就详细说一下UINavigationController的使用:
1、首先新建一个工程(就不多说了)创建RootViewController(继承自UIViewController)。
2、打开AppDelegate.h文件添加属性
3、打开AppDelegate.m文件的
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法添加navController和根视图(RootViewController)。
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch. self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
RootViewController *rootVC = [[RootViewController alloc] init];
rootVC.title = @"RootViewController"; //初始化navController
self.navController = [[UINavigationController alloc] init];
//给rootVC添加推入动作
[self.navController pushViewController:rootVC animated:YES];
//将navController加到window上
[self.window addSubview:self.navController.view]; [self.window makeKeyAndVisible]; return YES;
}
效果图:
4、添加UIBarButtonItem
注:UIBarButtonItem分为leftBarButtonItem和rightBarButtonItem;
#import "RootViewController.h" @interface RootViewController (){ UIBarButtonItem *leftButton;
UIBarButtonItem *rightButton; } @end @implementation RootViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view from its nib. leftButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemEdit target:self action:@selector(leftButtonAction:)];
self.navigationItem.leftBarButtonItem = leftButton; rightButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(rightButtonAction:)];
self.navigationItem.rightBarButtonItem = rightButton; }
效果图:
这里还要说一下initWithBarButtonSystemItem:即系统自带按钮风格:
UIBarButtonSystemItemDone:蓝色文字按钮,标有“Done”;
UIBarButtonSystemItemCancel:文字按钮,标有“Cancel”;
UIBarButtonSystemItemEdit:文字按钮,标有“Edit”;
UIBarButtonSystemItemSave:蓝色文字按钮,标有“Save”;
UIBarButtonSystemItemAdd:图像按钮,上面有一个Å符号;
UIBarButtonSystemItemFlexibleSpace:空白,占用空间大小可变;
UIBarButtonSystemItemFixedSpace:空白占位符;
UIBarButtonSystemItemCompose:图像按钮,上有一支笔和纸张;
UIBarButtonSystemItemReply:图像按钮,上有一个回复箭头;
UIBarButtonSystemItemAction:图像按钮,上有一个动作箭头;
UIBarButtonSystemItemOrganize:图像按钮,上有一个文件夹以及向下箭头;
UIBarButtonSystemItemBookmarks:图像按钮,上有书签图标;
UIBarButtonSystemItemSearch:图像按钮,上有spotlight图标;
UIBarButtonSystemItemRefresh:图像按钮,上有一个环形的刷新箭头;
UIBarButtonSystemItemStop:图像按钮,上有一个停止记号X;
UIBarButtonSystemItemCamera:图像按钮,上有一个照相机;
UIBarButtonSystemItemTrash:图像按钮,上有一个垃圾桶;
UIBarButtonSystemItemPlay:图像按钮,上有一个播放图标;
UIBarButtonSystemItemPause:图像按钮,上有一个暂停图标;
UIBarButtonSystemItemRewind:图像按钮,上有一个倒带图标;
UIBarButtonSystemItemFastForward:图像按钮,上有一个快进图标;
5、UIBarButtonItem的事件的实现
- (void)leftButtonAction:(UIBarButtonItem*)leftAction{ UIAlertView *leftAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"是否继续编辑" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
[leftAlert show];
} - (void)rightButtonAction:(UIBarButtonItem*)rightAction{ UIAlertView *rightAlert = [[UIAlertView alloc] initWithTitle:@"提示" message:@"退出" delegate:self cancelButtonTitle:@"是" otherButtonTitles:@"否", nil];
[rightAlert show]; }
效果图: