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

大熊猫猪·侯佩原创或翻译作品.欢迎转载,转载请注明出处.
假设认为写的不好请多提意见,假设认为不错请多多支持点赞.谢谢! 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];
}
posted @ 2017-06-20 20:36  clnchanpin  阅读(160)  评论(0编辑  收藏  举报