通过官方文档可以知道:navigation controller本身就是一个管理view controller层级的对象,但它最灵活的地方在于,可以跨界面跳转和很简单的实现自定义动画(参照http://iostrack.com/post/2012-08-17/40036208779)。
我们来看下navigation对栈的管理方法
– pushViewController:animated://推送显示新的viewController
– popViewControllerAnimated://返回到上一级的viewController
– popToRootViewControllerAnimated://返回到rootViewController
– popToViewController:animated://跳转到指定的viewController,实际上相当于返回到栈序列中的第几项,处于该项上面的实例将会被释放(收到dealloc事件)
各具一个实例吧:
– pushViewController:animated:
xib下:
DemoViewController *demoViewController = [[DemoViewController alloc] initWithNibName:@"DemoViewController" bundle:[NSBundle mainBundle]];
[self.navigationController pushViewController: demoViewController animated:YES];
storyboard下:
//指定是哪个storyboard下的哪个viewController
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"MainStoryboard" bundle:nil];
UIViewController *demoViewController1 = [sb instantiateViewControllerWithIdentifier:@"viewControllerIdentifier"]; //在view controller的attribute inspector下设置
[self.navigationController pushViewController: demoViewController1 animated:YES];
– popViewControllerAnimated:
[self.navigationControllerpopViewControllerAnimated:YES];
– popToRootViewControllerAnimated:
[self.navigationController popToRootViewControllerAnimated:YES];
– popToViewController:animated:
//跳转至链表中的某个位次
[self.navigationController popToViewController:[self.navigationController.viewControllers objectAtIndex:0] animated:YES];
//跳转至链表中的特点view controller
for (UIViewController *temp inself.navigationController.viewControllers) {
if ([temp isKindOfClass:[你要跳转到的Controller class]]) {
[self.navigationControllerpopToViewController:temp animated:YES];
}
}
(以上两个用法引自http://blog.csdn.net/hufeng825/article/details/7243823)
经过细测,这种方法本身可以满足在链表中来回跳动且无内存泄露。