#import "AppDelegate.h"
#import "MainViewController.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];
MainViewController *mainVC = [[MainViewController alloc] init];
// 导航控制器的使用
// 创建导航控制器的时候指定一个栈底控制器(rootVC)
UINavigationController *navi = [[UINavigationController alloc] initWithRootViewController:mainVC];
// 把导航控制器指定为window的根视图控制器
self.window.rootViewController = navi;
[mainVC release];
[navi release];
return YES;
}
#import "MainViewController.h"
#import "SecondViewController.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.title = @"第一页";
// 设置NavigationBar 导航栏
// 导航栏对每个导航控制器都是唯一的
// 获取导航栏对象
// 1. 改变导航栏的颜色
// self.navigationController.navigationBar.barTintColor = [UIColor greenColor];
// 2. 改变导航栏的半透明度效果,会相应的改变VC的view的坐标系起点
// self.navigationController.navigationBar.translucent = YES;
// 3. 隐藏导航栏
// self.navigationController.navigationBarHidden = YES;
// navigationItem
// 保存按钮信息的一个类,每个VC都有一个这个类的属性,用来保存显示在导航栏上的按钮信息
// 右侧的按钮 right
self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemTrash target:self action:@selector(barButtonAction:)] autorelease];
// 中间的自定义视图 titleView
// UISegmentedControl *seg = [[UISegmentedControl alloc] initWithItems:@[@"1",@"2"]];
// self.navigationItem.titleView = seg;
// [seg release];
self.view.backgroundColor = [UIColor redColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 120, 335, 40);
button.backgroundColor = [UIColor yellowColor];
[button setTitle:@"推出第二页" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)barButtonAction:(UIBarButtonItem *)item
{
NSLog(@"导航栏按钮");
}
- (void)buttonAction:(UIButton *)btn
{
NSLog(@"点击");
// 使用导航控制器push一个新的VC
// (约定VC代表视图控制器(viewController))
// 1.创建一个新的VC对象
SecondViewController *secondVC = [[SecondViewController alloc] init];
// 2.使用导航控制器push新的VC
[self.navigationController pushViewController:secondVC animated:YES];
// 3.内存管理
[secondVC release];
}
#import "ForthViewController.h"
@interface ForthViewController ()
@end
@implementation ForthViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor purpleColor];
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
button.frame = CGRectMake(20, 120, 335, 40);
button.backgroundColor = [UIColor yellowColor];
[button setTitle:@"返回" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:button];
}
- (void)buttonAction:(UIButton *)btn
{
// 使用导航控制器出栈(pop)
// 1.返回首页
// [self.navigationController popToRootViewControllerAnimated:YES];
// 2. 返回上一页
// [self.navigationController popViewControllerAnimated:YES];
// 3.返回栈里面的某一页
UIViewController *vc1 = [self.navigationController.viewControllers objectAtIndex:1];
[self.navigationController popToViewController:vc1 animated:YES];
}