导航控制器就是UINavigationController,app里会经常用到。
下面的图显示了导航控制器的流程。最左侧是根视图,当用户点击其中的General项时 ,General视图会滑入屏幕;当用户继续点击Auto-Lock项时,Auto-Lock视图将滑入屏幕。相应地,在对象管理上,导航控制器使用了导航堆栈。根视图控制器在堆栈最底层,接下来入栈的是General视图控制器和Auto-Lock视图控制器。可以调用pushViewControllerAnimated:方法将视图控制器推入栈顶,也可以调用popViewControllerAnimated:方法将视图控制器弹出栈。
UINavigationControllerUINavigationController有Navigation bar ,Navigation View ,Navigation toobar等组成:
UINavigationController组成下面我们写个例子演示一下:
首先我们新建一个controller,就叫AboutViewController吧。为了演示方便,我们也不放什么东西进去了。
然后重新设置view的层级关系,打开AppDelegate.h。再interface里新增一行:
@property (strong, nonatomic) UINavigationController *navController;
然后打开AppDelegate.m。修改application方法:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after application launch.
...
instance_rootView.title = @"main";
self.navController = [ [UINavigationController alloc] init];
[self.navController pushViewController:instance_rootView animated:YES];
[self.window addSubview:self.navController.view];
//将RootViewController设置为根视图控制器
//self.window.rootViewController = instance_rootView;
//设置为主窗口并可见
[self.window makeKeyAndVisible];
return YES;
}
再打开RootViewController.m。修改方法someButtonClicked:
-(void) someButtonClicked{
AboutViewController *controller = [[AboutViewController alloc] init];
[self.navigationController pushViewController:controller animated:YES];
/*
UIAlertView *alertView = [[UIAlertView alloc]
initWithTitle:@"Hello, World"
message:@"This is my first app!"
delegate:nil
cancelButtonTitle:@"ok"
otherButtonTitles:nil];
[alertView show];
*/
}
Run一下试试?
网友评论