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

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
如果觉得写的不好请多提意见,如果觉得不错请多多支持点赞.谢谢! 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];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值