Skip to content

Commit

Permalink
Notify a controller when its view appear
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanLage committed Jan 24, 2015
1 parent 9dd3afd commit c38d8da
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions SLPagingView/SLPagingViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ @interface SLPagingViewController () <UIScrollViewDelegate>
@property (nonatomic, strong) UIPageControl *pageControl;
@property (nonatomic, strong) UIView *navigationBarView;
@property (nonatomic, strong) NSMutableArray *navItemsViews;
@property (nonatomic, strong) NSMutableArray *controllerReferences;
@property (nonatomic) BOOL needToShowPageControl;
@property (nonatomic) BOOL isUserInteraction;
@property (nonatomic) NSInteger indexSelected;
Expand Down Expand Up @@ -110,6 +111,7 @@ -(id)initWithNavBarControllers:(NSArray *)controllers showPageControl:(BOOL)addP
-(id)initWithNavBarControllers:(NSArray *)controllers navBarBackground:(UIColor *)background showPageControl:(BOOL)addPageControl{
NSMutableArray *views = [[NSMutableArray alloc] initWithCapacity:controllers.count];
NSMutableArray *items = [[NSMutableArray alloc] initWithCapacity:controllers.count];
_controllerReferences = [[NSMutableArray alloc] initWithArray:controllers];
for(int i =0; i<controllers.count; i++){
// Be sure we got s subclass of UIViewController
if([controllers[i] isKindOfClass:UIViewController.class]){
Expand Down Expand Up @@ -145,6 +147,7 @@ -(id)initWithNavBarItems:(NSArray *)items controllers:(NSArray *)controllers sho

-(id)initWithNavBarItems:(NSArray *)items navBarBackground:(UIColor *)background controllers:(NSArray *)controllers showPageControl:(BOOL)addPageControl{
NSMutableArray *views = [[NSMutableArray alloc] initWithCapacity:controllers.count];
_controllerReferences = [[NSMutableArray alloc] initWithArray:controllers];
for(int i =0; i<controllers.count; i++){
// Be sure we got s subclass of UIViewController
if([controllers[i] isKindOfClass:UIViewController.class])
Expand All @@ -160,22 +163,51 @@ -(id)initWithNavBarItems:(NSArray *)items navBarBackground:(UIColor *)background

- (void)loadView {
[super loadView];
// Notify all conctrollers
[self notifyControllers:NSSelectorFromString(@"loadView")
object:nil
checkIndex:NO];
// Try to load controller from storyboard
[self loadStoryboardControllers];
// Set up the controller
[self setupPagingProcess];
}

-(void)viewWillAppear:(BOOL)animated{
[super viewWillAppear:animated];
// Be notify when the device's orientation change
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];
// Notify all conctrollers
[self notifyControllers:NSSelectorFromString(@"viewDidAppear:")
object:@(animated)
checkIndex:YES];
}

-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated];
// Notify all conctrollers
[self notifyControllers:NSSelectorFromString(@"viewDidAppear:")
object:@(animated)
checkIndex:YES];
}

-(void)viewWillDisappear:(BOOL)animated{
[super viewWillDisappear:animated];
// Notify all conctrollers
[self notifyControllers:NSSelectorFromString(@"viewWillDisappear:")
object:@(animated)
checkIndex:YES];
}

- (void)viewDidLoad{
[super viewDidLoad];
// Notify all conctrollers
[self notifyControllers:NSSelectorFromString(@"viewDidLoad")
object:nil
checkIndex:NO];
[self setCurrentIndex:self.indexSelected
animated:NO];
}
Expand Down Expand Up @@ -240,6 +272,8 @@ -(void)addViewControllers:(UIViewController *) controller needToRefresh:(BOOL) r
// Save the controller
[self.viewControllers setObject:controller.view
forKey:@(tag)];
// Save controller reference
[self.controllerReferences addObject:controller];
// Do we need to refresh the UI ?
if(refresh)
[self setupPagingProcess];
Expand All @@ -257,6 +291,7 @@ -(void) initCrucialObjects:(UIColor *)background showPageControl:(BOOL) showPage
_navigationSideItemsStyle = SLNavigationSideItemsStyleDefault;
_viewControllers = [NSMutableDictionary new];
_navItemsViews = [NSMutableArray new];
_controllerReferences = [NSMutableArray new];
}

// Load any defined controllers from the storyboard
Expand All @@ -280,6 +315,22 @@ - (void)loadStoryboardControllers
}
}

// Perform a specific selector for each controllers
-(void)notifyControllers:(SEL)selector object:(id)object checkIndex:(BOOL)index{
if(index && self.controllerReferences.count > self.indexSelected) {
[(UIViewController*)self.controllerReferences[self.indexSelected] performSelectorOnMainThread:selector
withObject:object
waitUntilDone:NO];
}
else{
[self.controllerReferences enumerateObjectsUsingBlock:^(UIViewController* ctr, NSUInteger idx, BOOL *stop) {
[ctr performSelectorOnMainThread:selector
withObject:object
waitUntilDone:NO];
}];
}
}

// Add a view as a navigationBarItem
-(void)addNavigationItem:(UIView*)v tag:(int)tag{
CGFloat distance = (SCREEN_SIZE.width/2) - self.navigationSideItemsStyle;
Expand Down Expand Up @@ -443,6 +494,10 @@ -(void)sendNewIndex:(UIScrollView *)scrollView{
if(self.didChangedPage)
self.didChangedPage(self.indexSelected);
}
// Try to notify the controller concerned
[self notifyControllers:NSSelectorFromString(@"viewDidAppear:")
object:@(YES)
checkIndex:YES];
}

#pragma mark - ScrollView delegate
Expand Down

0 comments on commit c38d8da

Please sign in to comment.