判断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;

你可能感兴趣的:(判断UIViewController消失是Pop还是Push)