Skip to content

Commit

Permalink
Fix observers/notifications lifecycle at OASuperViewController
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-osm committed Apr 24, 2024
1 parent ebf2d09 commit 2a69e78
Showing 1 changed file with 23 additions and 25 deletions.
48 changes: 23 additions & 25 deletions Sources/Controllers/OASuperViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,38 @@ @implementation OASuperViewController
#pragma mark - Initialization

// use addNotification:selector: method here
// notifications will be automatically added in viewWillAppear: and removed in dealloc
// notifications will be automatically added in viewDidLoad: and removed in dealloc
- (void)registerNotifications
{
}

// use addObserver: method here
// observers will be automatically added in viewDidLoad: and removed in dealloc
- (void)registerObservers
{
}

// do not override
- (void)addNotification:(NSNotificationName)name selector:(SEL)selector
{
[NSNotificationCenter.defaultCenter addObserver:self selector:selector name:name object:nil];
}

// use addObserver: method here
// observers will be automatically added in viewDidAppear: and removed in viewWillDisappear:
- (void)registerObservers
// do not override
- (OAAutoObserverProxy *)addObserver:(OAAutoObserverProxy *)observer
{
[_observers addObject:observer];
return observer;
}

- (void)registerNotificationsAndObservers
{
// for content size category
[self addNotification:UIContentSizeCategoryDidChangeNotification selector:@selector(onContentSizeChanged:)];
// for other
[self registerNotifications];

[self registerObservers];
}

- (void)unregisterNotificationsAndObservers
Expand All @@ -46,13 +63,6 @@ - (void)unregisterNotificationsAndObservers
[observer detach];
}

// do not override
- (OAAutoObserverProxy *)addObserver:(OAAutoObserverProxy *)observer
{
[_observers addObject:observer];
return observer;
}

#pragma mark - UIViewController

- (void)viewDidLoad
Expand All @@ -62,24 +72,12 @@ - (void)viewDidLoad
[self addAccessibilityLabels];

_observers = [NSMutableArray array];
}

- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear:animated];

// for content size category
[self addNotification:UIContentSizeCategoryDidChangeNotification selector:@selector(onContentSizeChanged:)];
// for other
[self registerNotifications];

[self registerObservers];
[self registerNotificationsAndObservers];
}

- (void)viewWillDisappear:(BOOL)animated
- (void) dealloc
{
[super viewWillDisappear:animated];

[self unregisterNotificationsAndObservers];
}

Expand Down

0 comments on commit 2a69e78

Please sign in to comment.