首先通过File - New - New Project 创建一个Single View Application
项目创建之后, 再通添加文件 - Coca Touch - UIViewController 创建MainViewController, 其中xib项需要选中.
同样的方法创建一个SubViewController.
切换回AppDelegate.m 添加
#import "MainViewController.h"
然后将didFinishLaunchingWithOptions的方法发为下面的. 主要是定义一个UINavigationController, 然后定义其的rootViewController为MainViewController
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
//self.viewController = [[NavViewController alloc] initWithNibName:@"NavViewController" bundle:nil];
//self.window.rootViewController = self.viewController;
self.viewController= [[NavViewController alloc] initWithNibName:@"NavViewController" bundle:nil];
UINavigationController *nav = [[UINavigationController alloc] initWithRootViewController:self.viewController];
[self.window addSubview:nav.view];
[self.window makeKeyAndVisible];
return YES;
}
然后, 返回MainViewController, 定义一个button, 定义buttonpressed事件.
返回nib, 拖一个按钮到window上面, 通过拖拽建立起关联.
-(IBAction)buttonPressed
{
self.sub = [[SubNavController alloc] initWithNibName:@"SubNavController" bundle:nil];
[self.navigationController pushViewController:self.sub animated:YES];
[self.sub release];
}
此时运行项目, 点击按钮, 就会切换到SubNavController中了~ 上面会显示出"返回"的按钮.
此外, Navigation拥有下面的属性, 都可以自己定义.
-title
-titleview
-backBarButtonItem
-rightBarButtonItem
-leftBarButtonItem