前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >UINavigationController的setViewControllers方法

UINavigationController的setViewControllers方法

作者头像
JoeyBlue
发布2021-09-07 14:42:19
8160
发布2021-09-07 14:42:19
举报
文章被收录于专栏:代码手工艺人

在iOS开发中,UINavigationController是很常用的Controller,对它的一般操作就像操作一个栈,push和pop。但也经常会遇到pop和push无法优雅的完成的操作,比如退回到中间的某个VC上,或者在第一个VC之前添加一个VC等,更甚者要重新构造整个VC的顺序,这时候setViewControllers方法就排上用场了,它使对VC栈的操作不再局限于push和pop,而是构造整个VC栈并应用到当前的UINavigationController中,这个方法支持iOS3.0+,放心使用。 #Sample

代码语言:javascript
复制
NSMutableArray * viewControllers = [self.navigationController.viewControllers mutableCopy];
[viewControllers removeLastObject];
[viewControllers addObject:newController];

[self.navigationController setViewControllers:viewControllers animated:YES];
// [viewControllers relase] // if non-arc

感谢 Allen(Weibo) 提供的代码和思路 #说明 下面这段摘自Api文档

代码语言:javascript
复制
You can use this method to update or replace the current view controller stack without pushing or popping each controller explicitly. In addition, this method lets you update the set of controllers without animating the changes, which might be appropriate at launch time when you want to return the navigation controller to a previous state.

If animations are enabled, this method decides which type of transition to perform based on whether the last item in the items array is already in the navigation stack. 

1.If the view controller is currently in the stack, but is not the topmost item, this method uses a pop transition; 
2.if it is the topmost item, no transition is performed. 
3.If the view controller is not on the stack, this method uses a push transition. 

Only one transition is performed, but when that transition finishes, the entire contents of the stack are replaced with the new view controllers. For example, if controllers A, B, and C are on the stack and you set controllers D, A, and B, this method uses a pop transition and the resulting stack contains the controllers D, A, and B.

Have fun!

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-05-242,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档