From cf578e0f4ffc7c52f5ce23adfe31c6866c33c9ab Mon Sep 17 00:00:00 2001 From: x x Date: Tue, 31 May 2016 01:26:38 +0300 Subject: [PATCH] Remove global state from EAIntroView's constructor The code that modifies global state has been moved into the willMoveIntoSuperView: method. This fixes a crash on iOS <= 8. It also fixes an issue where the device would generate Device Orientation Notifications when it shouldn't. Previously, if an EAIntroView was removed by removing its parent view (or ViewContrroller) - the cleanup code for the EAIntroView wasn't being called. The same issue existed for an EAIntroView that was created, but not added to a view hierarchy. Fixes #168 --- EAIntroView/EAIntroView.m | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/EAIntroView/EAIntroView.m b/EAIntroView/EAIntroView.m index 64bc476..36be525 100644 --- a/EAIntroView/EAIntroView.m +++ b/EAIntroView/EAIntroView.m @@ -80,13 +80,6 @@ - (void)applyDefaultsToSelfDuringInitializationWithFrame:(CGRect)frame pages:(NS self.pages = [pagesArray copy]; [self buildFooterView]; - - // Add observer for device orientation: - [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; - [[NSNotificationCenter defaultCenter] addObserver:self - selector:@selector(deviceOrientationDidChange:) - name:UIDeviceOrientationDidChangeNotification - object:nil]; } - (void)applyDefaultsToBackgroundImageView:(UIImageView *)backgroundImageView { @@ -168,10 +161,6 @@ - (void)checkIndexForScrollView:(EARestrictedScrollView *)scrollView { } - (void)finishIntroductionAndRemoveSelf { - // Remove observer for rotation - [[NSNotificationCenter defaultCenter] removeObserver:self]; - [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; - //prevent last page flicker on disappearing self.alpha = 0; @@ -205,6 +194,22 @@ - (void)notifyDelegateWithPreviousPage:(NSUInteger)previousPageIndex andCurrentP } } +-(void)willMoveToSuperview:(UIView *)newSuperview { + [super willMoveToSuperview:newSuperview]; + if (self.superview == nil && newSuperview != nil) { + // Add observer for device orientation: + [[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(deviceOrientationDidChange:) + name:UIDeviceOrientationDidChangeNotification + object:nil]; + } else if (self.superview != nil && newSuperview == nil) { + // Remove observer for rotation + [[NSNotificationCenter defaultCenter] removeObserver:self]; + [[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications]; + } +} + #pragma mark - Properties - (EARestrictedScrollView *)scrollView {