1:正常建立工程
2:新建一个objective-C类,命名(可以随便)RootViewController,可带可不带xib文件
3:appdelegate.h
#import <UIKit/UIKit.h> @class RootViewController; @interface AppDelegate : UIResponder <UIApplicationDelegate> @property (strong, nonatomic) UIWindow *window; @property (strong, nonatomic) RootViewController *rootviewcontroller; //实的 @property (strong,nonatomic) UINavigationController *navigationController; //虚的 @end
4:appdelegate.m
#import "AppDelegate.h" #import "RootViewController.h" @implementation AppDelegate @synthesize window = _window; @synthesize rootviewcontroller = _rootviewcontroller; @synthesize navigationController; - (void)dealloc { [_window release]; [_rootviewcontroller release]; [super dealloc]; } - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { //代码添加navagationController // self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // // Override point for customization after application launch. // self.viewController = [[[ViewController alloc] initWithNibName:@"ViewController" bundle:nil] autorelease]; // UINavigationController *navController = [[UINavigationController alloc] initWithRootViewController:self.viewController]; // // [self.window addSubview:navController.view]; // self.window.rootViewController = navController; // //self.window.rootViewController = self.viewController; // [self.window makeKeyAndVisible]; // return YES; // self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease]; // Override point for customization after application launch. [self.window makeKeyAndVisible]; self.rootviewcontroller = [[RootViewController alloc] initWithNibName:Nil bundle:NULL]; self.navigationController = [[UINavigationController alloc] initWithRootViewController:self.rootviewcontroller]; [self.window addSubview:navigationController.view]; return YES; }
RootViewController.m
// // RootViewController.m // Qin // // Created by jstv.com jstv on 12-6-11. // Copyright (c) 2012年 jstv.com. All rights reserved. // #import "RootViewController.h" #import "MapViewController.h" #import <QuartzCore/QuartzCore.h> @interface RootViewController () @end @implementation RootViewController - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { // Custom initialization } return self; } - (void)viewDidLoad { [super viewDidLoad]; //定制navagationbar //右边添加一个按钮 // UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithTitle:@"next" style:UIBarButtonItemStyleBordered target:self action:@selector(nav1_do)]; // self.navigationItem.rightBarButtonItem = item1; // [item1 release]; //右边添加多个按钮 UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 150, 45)]; [toolbar setTintColor:[self.navigationController.navigationBar tintColor]]; [toolbar setAlpha:[self.navigationController.navigationBar alpha]]; NSMutableArray *buttons = [[NSMutableArray alloc] initWithCapacity:2]; //Capacity,容量 UIBarButtonItem *btn1 = [[UIBarButtonItem alloc] initWithTitle:@"btn1title" style:UIBarButtonItemStyleBordered target:self action:@selector(btn1_do)]; UIBarButtonItem *btn2 = [[UIBarButtonItem alloc] initWithTitle:@"btn2title" style:UIBarButtonItemStyleBordered target:self action:@selector(btn2_do)]; [buttons addObject:btn1]; [btn1 release]; [buttons addObject:btn2]; [btn2 release]; [toolbar setItems:buttons animated:YES]; [buttons release]; UIBarButtonItem *myitem = [[UIBarButtonItem alloc] initWithCustomView:toolbar]; self.navigationItem.rightBarButtonItem = myitem; [myitem release]; [toolbar release]; [self setTitle:@"Page One"]; NSLog(@"来到了rootviewcontroller"); [self.navigationController setNavigationBarHidden:YES]; // Do any additional setup after loading the view from its nib. } -(void)nav1_do { MapViewController *mapview = [[MapViewController alloc] initWithNibName:Nil bundle:NULL]; //[self.navigationController pushViewController:mapview animated:YES]; //[self.navigationController popToViewController:mapview animated:YES]; [self.navigationController presentModalViewController:mapview animated:YES]; //从下往上弹出 } -(void)btn1_do { NSLog(@"btn1_do"); MapViewController *mapview = [[MapViewController alloc] initWithNibName:Nil bundle:NULL]; [self.navigationController pushViewController:mapview animated:YES]; } -(void)btn2_do { NSLog(@"btn2_do"); } - (void)viewDidUnload { [super viewDidUnload]; // Release any retained subviews of the main view. // e.g. self.myOutlet = nil; } - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation == UIInterfaceOrientationPortrait); } - (IBAction)goMapview { //presentModalViewController切换 MapViewController *mapview = [[MapViewController alloc] initWithNibName:Nil bundle:NULL]; [self.navigationController presentModalViewController:mapview animated:YES]; } - (IBAction)CustomPushview { //自定义切换动画效果 CATransition *animation = [CATransition animation]; [animation setDuration:3.3]; [animation setType:kCATransitionFade]; //淡入淡出 [animation setSubtype:kCATransitionFromLeft]; [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]]; MapViewController *mapview = [[MapViewController alloc] initWithNibName:Nil bundle:NULL]; [self.navigationController pushViewController:mapview animated:NO]; [self.navigationController.view.layer addAnimation:animation forKey:nil]; } @end