AppDelegate.h
#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end
AppDelegate.m
#import "AppDelegate.h"
#import "RootViewController.h"
@interface AppDelegate ()
@end
@implementation AppDelegate
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.window.backgroundColor = [UIColor whiteColor];
[self.window makeKeyAndVisible];
[_window release];
// 先创建一个视图控制器对象
RootViewController *rootVC = [[RootViewController alloc] init];
// 创建一个导航视图控制器
UINavigationController *naVC = [[UINavigationController alloc] initWithRootViewController:rootVC];
// 指定导航视图控制器成为window的根视图控制器
self.window.rootViewController = naVC;
// 内存管理
[rootVC release];
[naVC release];
return YES;
}
RootViewController.h
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController
@end
RootViewController.m
#import "RootViewController.h"
#import "SecondViewController.h"
@interface RootViewController ()
@end
@implementation RootViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor yellowColor];
// 通过两方面去设置naVC的样式
// 第一部分: 外观
// 修改背景颜色, 不是所有的背景颜色都是backgroundColor
// 设置成不透明, YES默认是半透明
self.navigationController.navigationBar.translucent = NO;
#warning 导航视图控制器里的navigationBar默认是半透明效果, 但是不透明效果会修改坐标系的起点, 如果想要半透明效果, 为了让视图全部显示, 需要把坐标向下平移64
// self.navigationController.navigationBar.barTintColor = [UIColor redColor];
// UIScrollView *view = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];
// view.backgroundColor = [UIColor blueColor];
// [self.view addSubview:view];
// [view release];
// 第二部分: 内容
// 添加标题
self.title = @"天琴圣域";
// self.navigationItem.title = @"网易新闻";
// 中心位置添加一个视图
// 创建一个segment
UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"通话", @"信息"]];
self.navigationItem.titleView = seg;
// 创建左右两边两个按钮
// 采用了系统默认的按钮样式进行创建
self.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCamera target:self action:@selector(click:)] autorelease];
// self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"moxie.png"] style:UIBarButtonItemStylePlain target:self action:@selector(click:)] autorelease ];
// 把按钮内容设置成文字, initWithTitle, 中文可能会让代码提示不出来
// self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithTitle:@"测试" style:UIBarButtonItemStylePlain target:self action:@selector(click:)] autorelease];
UIButton *itemButton = [UIButton buttonWithType:UIButtonTypeCustom];
[itemButton setImage:[UIImage imageNamed:@"shuangzi.png"] forState:UIControlStateNormal];
itemButton.frame = CGRectMake(0, 0, 40, 40);
// 如果不使用自定义的视图, 图片都是蓝色的, 只有自定义的视图才能正常显示图片样式
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:itemButton] autorelease];
// 外观设置通过navigationBar来操作, 能设置半透明和颜色(bartintcolor)
// 内容通过navigationitem来操作, 可以设置左右按钮, 中间标题title, titleView
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 150, 50);
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)button {
// // 用模态方式
// SecondViewController *secVC = [[SecondViewController alloc] init];
// // 设置模态动画
// [secVC setModalTransitionStyle:0];
// [self presentViewController:secVC animated:YES completion:nil];
// [secVC release];
// push下一页面
// 先创建目标页面对象
SecondViewController *secVC = [[SecondViewController alloc] init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
NSLog(@"%@", self.navigationController);
}
- (void)click:(UIBarButtonItem *)item {
NSLog(@"点击ing");
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
SecondViewController.h
#import <UIKit/UIKit.h>
@interface SecondViewController : UIViewController
@end
SecondViewController.m
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第二页";
self.view.backgroundColor = [UIColor darkGrayColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 150, 50);
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)button {
ThirdViewController *thiVC = [[ThirdViewController alloc] init];
[self.navigationController pushViewController:thiVC animated:YES];
[thiVC release];
NSLog(@"%@", self.navigationController);
}
ThirdViewController.h
#import <UIKit/UIKit.h>
@interface ThirdViewController : UIViewController
@end
ThirdViewController.m
#import "ThirdViewController.h"
#import "FourthViewController.h"
@interface ThirdViewController ()
@end
@implementation ThirdViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第三页";
self.view.backgroundColor = [UIColor brownColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 150, 50);
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)button {
FourthViewController *fourVC = [[FourthViewController alloc] init];
[self.navigationController pushViewController:fourVC animated:YES];
[fourVC release];
NSLog(@"%@", self.navigationController.viewControllers);
}
FourthViewController.h
#import <UIKit/UIKit.h>
@interface FourthViewController : UIViewController
@end
FourthViewController.m
#import "FourthViewController.h"
#import "FifthViewController.h"
@interface FourthViewController ()
@end
@implementation FourthViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第四页";
self.view.backgroundColor = [UIColor cyanColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"下一页" forState:UIControlStateNormal];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 150, 50);
[self.view addSubview:button];
}
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSLog(@"%@", self.navigationController.viewControllers);
}
- (void)buttonAction:(UIButton *)button {
FifthViewController *fiveVC = [[FifthViewController alloc] init];
[self.navigationController pushViewController:fiveVC animated:YES];
[fiveVC release];
NSLog(@"%@", self.navigationController.viewControllers);
}
FifthViewController.h
#import <UIKit/UIKit.h>
@interface FifthViewController : UIViewController
@end
FifthViewController.m
#import "FifthViewController.h"
@interface FifthViewController ()
@end
@implementation FifthViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第五页";
self.view.backgroundColor = [UIColor lightGrayColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"返回" forState:UIControlStateNormal];
button.layer.borderWidth = 1;
button.layer.cornerRadius = 10;
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(100, 100, 150, 50);
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)button {
// [self.navigationController popToRootViewControllerAnimated:YES];
[self.navigationController popViewControllerAnimated:YES];
}