APPdelegatew文件
#import "TestAppDelegate.h"
#import "FirstViewController.h"
@implementation TestAppDelegate
- (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];
//导航视图控制器
//1创建导航控制器(UINavigationController)
FirstViewController *firstVC = [[FirstViewController alloc]init];
//创建导航控制器时 要制定一个视图控制器(rootViewController)
UINavigationController *naviVC = [[UINavigationController alloc]initWithRootViewController:firstVC];
//把导航控制器指定为window的根视图控制器(windoe的rootViewController)
self.window.rootViewController = naviVC;
//内存管理
[naviVC release];
[firstVC release];
[_window release];
return YES;
}
- (void)dealloc
{
[_window release];
[super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
// Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
FirstViewController文件
#import "FirstViewController.h"
#import "SecondViewController.h"
@interface FirstViewController ()
@end
@implementation FirstViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"皮城女警";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
//按钮用于推出新的视图控制器
UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
[button setTitle:@"让子弹飞一会吧" forState:UIControlStateNormal];
[button addTarget:self action:@selector(buttonclicked:) forControlEvents:UIControlEventTouchUpInside];
button.frame = CGRectMake(20, 120, 280, 50);
button.backgroundColor = [UIColor whiteColor];
[self.view addSubview:button];
//navigationBar的设置
//1.获得当前的navigationbar
//透明设置 YES/NO
[self.navigationController.navigationBar setTranslucent:NO];
//2.设置颜色
[self.navigationController.navigationBar setBarTintColor:[UIColor yellowColor]];
//3.隐藏navigationbar YES/NO
[self.navigationController setNavigationBarHidden:NO];
//navigationItm设置
//1.获取navigationItm
//设置标题
// self.navigationItem.title = @"冰霜女巫";
//设置标题位置的view
//self.navigationItem.titleView = button;
//设置navigation左侧的按钮(UIBarButtonItm)
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barButtonAction:)];
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemPlay target:(self) action:@selector(barButtonRight:)];
// UIBarButtonItem *barButton = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(barButtonAction:)];
// UIBarButtonItem *barButton1 = [[UIBarButtonItem alloc]initWithBarButtonSystemItem:UIBarButtonSystemItemBookmarks target:self action:@selector(barButtonAction:)];
// //将多个barButtonItem添加到数组
// NSArray *arr = [NSArray arrayWithObjects:barButton1, barButton2, nil];
// //将数组里面的所有item显示到navigationBar上
// self.navigationItem.leftBarButtonItems = arr;
}
- (void)barButtonRight:(UIBarButtonItem *)bar
{
SecondViewController *secVC = [[SecondViewController alloc]init];
[self.navigationController pushViewController:secVC animated:YES];
[secVC release];
}
- (void)barButtonAction:(UIBarButtonItem *)bar
{
NSLog(@"黎明就在眼前");
}
- (void)buttonclicked:(UIButton *)button
{
//推出第二个视图控制器
//1.创建第二个视图控制器
SecondViewController *secondVC = [[SecondViewController alloc]init];
//2.推出
//利用viewController的属性 获得当前的导航控制器
//调用pushViewController:animated:方法 推出新的viewController
[self.navigationController pushViewController:secondVC animated:YES];
//内存管理
[secondVC release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
SecondViewController文件
#import "SecondViewController.h"
#import "ThirdViewController.h"
@interface SecondViewController ()
@end
@implementation SecondViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
self.title = @"曙光女神";
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor redColor];
UIButton *abutton = [UIButton buttonWithType:UIButtonTypeSystem];
[abutton setTitle:@"太阳照耀着你" forState:UIControlStateNormal];
[abutton addTarget:self action:@selector(abuttonclicked:) forControlEvents:UIControlEventTouchUpInside];
abutton.frame = CGRectMake(22, 120, 280, 50);
abutton.backgroundColor = [UIColor whiteColor];
[self.view addSubview:abutton];
}
- (void)abuttonclicked:(UIButton *)button
{
ThirdViewController *thirdVC = [[ThirdViewController alloc]init];
[self.navigationController pushViewController:thirdVC animated:YES];
[thirdVC release];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
/*
#pragma mark - Navigation
// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
// Get the new view controller using [segue destinationViewController].
// Pass the selected object to the new view controller.
}
*/
@end
IOS导航控制器的随堂笔记
最新推荐文章于 2023-08-16 15:42:40 发布