UIViewController的 dismissViewControllerAnimated:completion:方法

转载自:http://blog.youkuaiyun.com/terrylee_cold/article/details/7529132

 今天coding的时候碰到了dismissModalViewControllerAnimated:方法.这个方法的功能是释放接受到这个消息的对象(UIViewcontroller对象).

    如:[self  dismissModalViewControllerAnimated:YES]; //animated的参数代表UIViewcontroller是否动态移除屏幕,默认是向屏幕下方移除屏幕外.

    对象调用这个方法之后,直接被dealloc,如再调用这个对象的view,这个对象重的 viewDidLoad方法会被调用.

    另外,在ios5.0之后,dismissModalViewControllerAnimated方法被 dismissViewControllerAnimated:completion:方法所取代.后者比前者多了一个(void (^)(void))completion参数,这个参数是一个block用来提供UIViewcontroller对象被释放之后运行的回调.这个block的回调方法是在UIViewcontroller的viewDidDisappear: 之后被调用.


    与以上的两个方法对应的方法是presentModalViewController:animated:和presentViewController:animated:completion:.




切克闹项目中上的代码是:

[self dismissViewControllerAnimated:YES completion:^{

        [GNNotification notificatonPost:GN_NOTI_RELOGIN_WITHUI object:nil userInfo:nil];

        [GNNotification notificatonPost:GN_NOTI_CLEARDATA_FROMMEMORY object:nil userInfo:nil];

    }];


#import "TPNavigationController.h" #import "TPContainerViewController.h" #import "TPContainerNavigationController.h" #import "UIViewController+TPNavigationExtension.h" #import <TPFoundation/TPUIUtils.h> @implementation TPContainerNavigationController + (instancetype)wrapNavigationControllerWithViewController:(UIViewController *)viewController { return [[self alloc] initWithViewController:viewController]; } - (instancetype)initWithViewController:(UIViewController *)viewController { if (self = [super init]) { self.viewControllers = @[viewController]; } return self; } - (void)viewDidLoad { [super viewDidLoad]; // 禁用包装的vc的返回手势 self.interactivePopGestureRecognizer.enabled = NO; } - (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated { TPContainerViewController *fromContainer = (TPContainerViewController *)self.navigationController.topViewController; if (![self shouldKeepOrientationFromController:[fromContainer rootViewController] toController:viewController]) { [self ensureOrientationIsPortrait]; } // push一个包装后的控制器 TPContainerViewController *wrapViewController = [TPContainerViewController wrapViewController:viewController]; if (self.tpNavigationController.viewControllers.count >= 1) { if (@available(iOS 9.0, *)) { wrapViewController.hidesBottomBarWhenPushed = YES; viewController.hidesBottomBarWhenPushed = YES; } else { // for iOS 8, set tabbar hidden [[self tabBarController].tabBar setHidden:YES]; } } //使用Xcode12.0.1编译的App,在iOS14上运行时,当调用popToRootViewControllerAnimated:YES到vc栈中非相邻的vc时,会发现rootViewController的tabbar消失了,如果是popToRootViewControllerAnimated:NO则没问题 // 应该是官方的bug,先复写该方法解决 else { if (@available(iOS 14.0, *)) { viewController.hidesBottomBarWhenPushed = NO; } } [self.navigationController pushViewController:wrapViewController animated:animated]; } - (UIViewController *)popViewControllerAnimated:(BOOL)animated { NSInteger controllersCount = self.navigationController.viewControllers.count; if (controllersCount > 1) { TPContainerViewController *fromContainer = (TPContainerViewController *)self.navigationController.topViewController; TPContainerViewController *toContainer = self.navigationController.viewControllers[controllersCount - 2]; if (![self shouldKeepOrientationFromController:[fromContainer rootViewController] toController:[toContainer rootViewController]]) { [self ensureOrientationIsPortrait]; } } if (self.navigationController) { return [self.navigationController popViewControllerAnimated:animated]; } return [super popViewControllerAnimated:animated]; } - (NSArray<UIViewController *> *)popToRootViewControllerAnimated:(BOOL)animated { [self ensureOrientationIsPortrait]; return [self.navigationController popToRootViewControllerAnimated:animated]; } - (NSArray<UIViewController *> *)popToViewController:(UIViewController *)viewController animated:(BOOL)animated { [self ensureOrientationIsPortrait]; TPNavigationController *tpNavigationController = viewController.tpNavigationController; // pop到指定vc时应找到对应的tpContainerViewController NSInteger index = [tpNavigationController.tpChildViewControllers indexOfObject:viewController]; return [self.navigationController popToViewController:tpNavigationController.viewControllers[index] animated:animated]; } - (void)presentViewController:(UIViewController *)viewControllerToPresent animated:(BOOL)flag completion:(void (^)(void))completion { [self ensureOrientationIsPortrait]; [self.navigationController presentViewController:viewControllerToPresent animated:flag completion:completion]; } - (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion { [self ensureOrientationIsPortrait]; [self.navigationController dismissViewControllerAnimated:flag completion:completion]; } - (void)setDelegate:(id<UINavigationControllerDelegate>)delegate { self.navigationController.delegate = delegate; } - (void)ensureOrientationIsPortrait { if (UIInterfaceOrientationIsPortrait([TPUIUtils getScreenOrientation])) { return; } if (@available(iOS 16.0, *)) { [self setNeedsUpdateOfSupportedInterfaceOrientations]; } [TPUIUtils changeScreenOrientationTo:UIInterfaceOrientationPortrait]; } - (BOOL)shouldKeepOrientationFromController:(UIViewController *)fromController toController:(UIViewController *)toController { SEL selector = @selector(controllersNeedToKeepOrientationWhenNavigating); if ([fromController respondsToSelector:selector]) { #pragma clang diagnostic push #pragma clang diagnostic ignored "-Warc-performSelector-leaks" NSArray *controllers = [fromController performSelector:selector]; #pragma clang diagnostic pop NSString *toControllerName = NSStringFromClass([toController class]); if ([controllers containsObject:toControllerName]) { return YES; } } return NO; } @end
最新发布
09-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值