<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(75, 209, 87);">
</p><p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(75, 209, 87);">AppDelegate.m</p>
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
// 初始化导航控制的根控制器
ViewController *vc = [[ViewController alloc] init];
// 初始化导航控制器
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:vc];
[vc release];
// 显示工具栏
nav.toolbarHidden = NO;
// 导航栏和工具栏的外观是全局定义的
// 配置颜色
nav.navigationBar.barTintColor = [UIColor redColor];
nav.toolbar.barTintColor = [UIColor redColor];
// 配置按钮主色调
nav.navigationBar.tintColor = [UIColor whiteColor];
nav.toolbar.tintColor = [UIColor whiteColor];
// 配置背景图
[nav.navigationBar setBackgroundImage:[UIImage imageNamed:@"nav_bg_ios7"] forBarMetrics:UIBarMetricsDefault];
[nav.toolbar setBackgroundImage:[UIImage imageNamed:@"nav_bg"] forToolbarPosition:UIBarPositionBottom barMetrics:UIBarMetricsDefault];
// 配置是否透明
nav.navigationBar.translucent = YES;
nav.toolbar.translucent = YES;
// 配置导航栏标题文本显示
// NSFontAttributeName:文本字体类型key
// NSForegroundColorAttributeName:文本色key
// NSShadowAttributeName:文本阴影key
NSShadow *shadow = [[NSShadow alloc] init];
shadow.shadowColor = [UIColor blackColor];
shadow.shadowOffset = CGSizeMake(5, 5);
NSDictionary *titleTextAttributes = @{NSFontAttributeName: [UIFont systemFontOfSize:17], NSForegroundColorAttributeName: [UIColor whiteColor], NSShadowAttributeName: shadow};
[shadow release];
nav.navigationBar.titleTextAttributes = titleTextAttributes;
// 显示导航控制器
// 一旦使用了导航控制器包装内容控制器,显示时优先显示导航控制器。
self.window.rootViewController = nav;
[nav release];
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
return YES;
}
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(75, 209, 87);">ViewController.m</p>
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: 'Heiti SC Light'; color: rgb(75, 209, 87);"><span style="font-family: Menlo;">/* </span>配置导航栏标题<span style="font-family: Menlo;"> */</span></p>
<pre name="code" class="objc">// 1.配置控制器标题
self.title = title;
// 2.配置导航栏标题(直接设置)
// UINavigationItem:所有导航栏的配置,包括标题和按钮,都是基于该对象。
self.navigationItem.title = @"首页";
<p style="margin-top: 0px; margin-bottom: 0px; font-size: 18px; font-family: Menlo; color: rgb(75, 209, 87);">/* <span style="font-family: 'Heiti SC Light';">配置工具栏</span> */</p>
// 导航控制器每一个页面的导航栏和工具栏之上按钮或者标题都是独立存在的,需要基于每一个单独的页面取定制。
// 使用UIBarButtonItem来配置导航栏和工具栏按钮
// 返回根页面
UIBarButtonItem *backToRootItem = [[UIBarButtonItem alloc] initWithTitle:@"Back To Root" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonPressed:)];
backToRootItem.tag = 10;
// 下一页
UIBarButtonItem *nextItem = [[UIBarButtonItem alloc] initWithTitle:@"Next" style:UIBarButtonItemStylePlain target:self action:@selector(barButtonPressed:)];
nextItem.tag = 11;
// 可延展空间控件
UIBarButtonItem *flexibleItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
self.toolbarItems = @[backToRootItem, flexibleItem, nextItem];
[backToRootItem release];
[flexibleItem release];
[nextItem release];
/* 定制导航栏返回按钮 */
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:[NSString stringWithFormat:@"返回%u", count] style:UIBarButtonItemStylePlain target:nil action:NULL];
self.navigationItem.backBarButtonItem = backItem;
[backItem release];
/* 定制导航栏按钮 */
// 刷新
UIBarButtonItem *refreshItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemRefresh target:self action:@selector(barButtonPressed:)];
refreshItem.tag = 12;
// 分享
UIBarButtonItem *actionItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(barButtonPressed:)];
actionItem.tag = 13;
self.navigationItem.rightBarButtonItems = @[actionItem, refreshItem];
[actionItem release];
[refreshItem release];
<pre name="code" class="objc">- (void)refresh {
// 初始化警告框UIAlertiew
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"温馨提示" message:@"网络异常,请稍后再试!" delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", @"重试", @"稍后再试", nil];
// 显示警告框
[alertView show];
[alertView release];
}
- (void)share {
// 初始化操作表单UIActionSheet
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"分享" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"注销" otherButtonTitles:@"新浪微博", @"腾讯微博", nil];
// 显示操作表单
[actionSheet showInView:self.view];
[actionSheet release];
}
#pragma mark - UIAlertViewDelegate methods
// 用户点击按钮后调用
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"alet view button index = %d", buttonIndex);
}
#pragma mark - UIActionSheetDelegate methods
// 按钮点击时调用
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
NSLog(@"action sheet button index = %d", buttonIndex);
}