您的位置:首页 > 其它

手动操作导航控制器的子视图控制器的数组

2016-02-28 10:15 351 查看

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.

如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! hopy ;)




免责申明:本博客提供的所有翻译文章原稿均来自互联网,仅供学习交流之用,请勿进行商业用途。同时,转载时不要移除本申明。如产生任何纠纷,均与本博客所有人、发表该翻译稿之人无任何关系。谢谢合作!



你可能希望直接操作与特定导航控制器相关的子视图控制器的数组.

你可以使用UINavigationController的viewControllers属性修改其相关联的子视图控制器:

- (void) goBack{
/* Get the current array of View Controllers */
NSArray *currentControllers = self.navigationController.viewControllers;
/* Create a mutable array out of this array */
NSMutableArray *newControllers = [NSMutableArray
arrayWithArray:currentControllers];
/* Remove the last object from the array */
[newControllers removeLastObject];
/* Assign this array to the Navigation Controller */
self.navigationController.viewControllers = newControllers;
}


你可以任何视图控制器中调用该方法去弹出继承体系中最后(最后push入的,猫猪注)的导航控制器的子视图控制器.

UINavigationController类的实例都持有一个数组对象,其中每一个元素皆为一个UIViewController对象.在获取到该数组之后,你可以以你希望的任何方式手动操作该数组.比如说,你可以删除该继承体系数组中的一个视图控制器.

但是直接操作这个UIViewController的数组并不会呈现给用户动画效果,如果你希望操作动画化,可以使用UINavigationController类的setViewControllers:animated:方法,代码如下:

- (void) goBack{
/* Get the current array of View Controllers */
NSArray *currentControllers = self.navigationController.viewControllers;
/* Create a mutable array out of this array */
NSMutableArray *newControllers = [NSMutableArray
arrayWithArray:currentControllers];
/* Remove the last object from the array */
[newControllers removeLastObject];
/* Assign this array to the Navigation Controller with animation */
[self.navigationController setViewControllers:newControllers
animated:YES];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: