判断UIViewController消失是Pop还是Push

判断一个ViewController的viewWillDisappear方式是Pop消失,还是Push被遮盖

判断一个ViewController的viewWillAppear方式是Pop消失,还是Push被遮盖

废话少说,根据这4个属性的描述

/*
  These four methods can be used in a view controller's appearance callbacks to determine if it is being
  presented, dismissed, or added or removed as a child view controller. For example, a view controller can
  check if it is disappearing because it was dismissed or popped by asking itself in its viewWillDisappear:
  method by checking the expression ([self isBeingDismissed] || [self isMovingFromParentViewController]).
*/

@property(nonatomic, readonly, getter=isBeingPresented) BOOL beingPresented API_AVAILABLE(ios(5.0));
@property(nonatomic, readonly, getter=isBeingDismissed) BOOL beingDismissed API_AVAILABLE(ios(5.0));

@property(nonatomic, readonly, getter=isMovingToParentViewController) BOOL movingToParentViewController API_AVAILABLE(ios(5.0));
@property(nonatomic, readonly, getter=isMovingFromParentViewController) BOOL movingFromParentViewController API_AVAILABLE(ios(5.0));

简单说!!就是,

  • Present&Dismiss时,可以在viewWillAppear||viewWillDisappear中,检查beingPresented &beingDismissed

  • Push&Pop时,只有添加和移除时才会让movingToParentViewController&movingFromParentViewController产生变化

rootVC push frontVC

//frontVC 的viewWillAppear - 新添加的VC的movingToParentViewController 为true
self.movingToParentViewController == true;

frontVC push backVC

//frontVC 的viewWillDisAppear - 已添加过的VC毫无波澜
self.movingToParentViewController == nil;
self.movingFromParentViewController == nil;

// backVC的viewWillAppear - 新添加的VC的movingToParentViewController 为true
self.movingToParentViewController == true;

backVC pop

// backVC的viewWillDisAppear - 将被移除的VC的movingFromParentViewController为true
self.movingFromParentViewController == true;
//frontVC的viewWillAppear - 已经添加过,仅仅是再次被显示的VC的状态没有变化
self.movingToParentViewController == nil;
self.movingFromParentViewController == nil;

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容