新建工程,创建三个类继承自UIViewController,分别是FirstViewController、SecondViewController 、ThirdViewController
一、编写APPDelegate.m文件中代码:
#import "AppDelegate.h"
#import "FirstViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
[selfsetWindow:[[UIWindowalloc]initWithFrame:[UIScreenmainScreen].bounds]];
[self.windowmakeKeyAndVisible];
FirstViewController *firstController = [[FirstViewControlleralloc]init];
//导航栏控制器,需要指定一个根视图控制器
//导航栏控制器主要用来管理子视图。
UINavigationController *navController = [[UINavigationControlleralloc]initWithRootViewController:firstController];
//给控制器设置背景颜色,一般不需要这么做
[navController.viewsetBackgroundColor:[UIColorwhiteColor]];
[self.windowsetRootViewController:navController];
returnYES;
}
@end
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfsetTitle:@"First"];
[self.viewsetBackgroundColor:[UIColorgrayColor]];
//给导航栏添加按钮,这里选用系统自带的UIBarButtonSystemItemAdd类型的按钮
UIBarButtonItem *item1 = [[UIBarButtonItemalloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAddtarget:selfaction:NULL];
//添加一个自定义的按钮样式
UIButton *itemButton = [UIButtonbuttonWithType:UIButtonTypeCustom];
//起始坐标被限制,所以一般就写为(0,0)
[itemButton setFrame:CGRectMake(0,0,80,30)];
[itemButton setBackgroundColor:[UIColorlightGrayColor]];
[itemButton setTitle:@"Item"forState:UIControlStateNormal];
[itemButton setTitle:@""forState:UIControlStateHighlighted];
UIBarButtonItem *item2 = [[UIBarButtonItemalloc]initWithCustomView:itemButton];
//
// self.navigationItem.leftBarButtonItem = item1;
//添加多个按钮,传入数组类型
self.navigationItem.leftBarButtonItems =@[item1, item2];
//给导航栏设置背景颜色,默认有一层毛玻璃特效
self.navigationController.navigationBar.backgroundColor = [UIColorblueColor];
//给导航栏设置背景图片
/*渲染样式
UIBarMetricsDefault:原样显示图片
UIBarMetricsCompact:如果已经设置颜色将显示设置的颜色,如果没有设置就用当前子视图的颜色渲染
*/
[self.navigationController.navigationBarsetBackgroundImage:[UIImageimageNamed:@"bg_navBar"]forBarMetrics:UIBarMetricsDefault];
//创建另一个Button
//当导航栏有背景图片后,按钮将frame坐标从导航栏下方开始计算
//当只是设置背景颜色后,按钮是从左上角开始计算坐标
UIButton *button = [UIButtonbuttonWithType:UIButtonTypeCustom];
[button setFrame:CGRectMake(100,80,100,30)];
[button setBackgroundColor:[UIColorblackColor]];
[button setTitle:@"Push"forState:UIControlStateNormal];
[button setTitle:@""forState:UIControlStateHighlighted];
[button setTitleColor:[UIColorwhiteColor]forState:UIControlStateNormal];
//给按钮绑定点击事件
[button addTarget:selfaction:@selector(pushController)forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
//不同视图控制器之间的跳转
-(void)pushController
{
//跳转到下一个视图控制器动画效果animated:传入YES or NO
[self.navigationControllerpushViewController:[[SecondViewControlleralloc]init]animated:YES];
}
@end
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfsetTitle:@"Second"];
[self.viewsetBackgroundColor:[UIColoryellowColor]];
//设置导航栏标题的颜色
[self.navigationController.navigationBarsetTitleTextAttributes:@{NSForegroundColorAttributeName: [UIColorwhiteColor]}];
UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithTitle:@"POP"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(popController)];
self.navigationItem.leftBarButtonItem = leftItem;
UIBarButtonItem *rightItem = [[UIBarButtonItemalloc]initWithTitle:@"Push"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(pushController)];
self.navigationItem.rightBarButtonItem = rightItem;
}
-(void)popController
{
[self.navigationControllerpopViewControllerAnimated:YES];
}
-(void)pushController
{
[self.navigationControllerpushViewController:[[ThirdViewControlleralloc]init]animated:YES];
}
@end
#import "ThirdViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[superviewDidLoad];
[selfsetTitle:@"Third"];
[self.viewsetBackgroundColor:[UIColorwhiteColor]];
UIBarButtonItem *leftItem = [[UIBarButtonItemalloc]initWithTitle:@"POP"style:UIBarButtonItemStylePlaintarget:selfaction:@selector(popController)];
self.navigationItem.leftBarButtonItem = leftItem;
}
-(void)popController
{
//返回到上一个界面
// [self.navigationController popViewControllerAnimated:YES];
//返回到主视图界面
[self.navigationControllerpopToRootViewControllerAnimated:YES];
//获取子视图控制器
// NSLog(@"%@", self.navigationController.viewControllers);
//跳转到指定子视图控制器对象
// [self.navigationController popToViewController:self.navigationController.viewControllers[1] animated:YES];
}
@end