Skip to content
This repository has been archived by the owner on Apr 27, 2021. It is now read-only.

Commit

Permalink
Remove global state from EAIntroView's constructor
Browse files Browse the repository at this point in the history
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 ealeksandrov#168
  • Loading branch information
x1024 authored and JakeSc committed Jul 18, 2016
1 parent 9bbb8f9 commit cf578e0
Showing 1 changed file with 16 additions and 11 deletions.
27 changes: 16 additions & 11 deletions EAIntroView/EAIntroView.m
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit cf578e0

Please sign in to comment.