From 9d09b94a7149937e9ee5feb15a0639e35278ff6c Mon Sep 17 00:00:00 2001 From: Romy Ilano Date: Fri, 19 Jun 2015 14:27:09 -0700 Subject: [PATCH] added delegate protocol --- .../YSLChildViewControllerDelegate.h | 20 +++++++++++++++++++ .../YSLContainerViewController.h | 3 ++- .../YSLContainerViewController.m | 9 +++++++++ .../project.pbxproj | 2 ++ .../contents.xcworkspacedata | 7 +++++++ .../ArtistsViewController.h | 3 +++ .../ArtistsViewController.m | 3 ++- .../ArtistsViewController.xib | 5 +++-- .../DetailViewController.h | 3 +++ .../PlayListTableViewController.h | 3 +++ .../PlayListTableViewController.xib | 5 +++-- .../SampleViewController.h | 3 +++ .../SampleViewController.m | 9 ++++++++- .../SampleViewController.xib | 4 ++-- .../ViewController.m | 7 +++++++ 15 files changed, 77 insertions(+), 9 deletions(-) create mode 100644 YSLContainerViewController/YSLChildViewControllerDelegate.h create mode 100644 YSLContainerViewControllerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata diff --git a/YSLContainerViewController/YSLChildViewControllerDelegate.h b/YSLContainerViewController/YSLChildViewControllerDelegate.h new file mode 100644 index 0000000..bd7edbd --- /dev/null +++ b/YSLContainerViewController/YSLChildViewControllerDelegate.h @@ -0,0 +1,20 @@ +// +// YSLChildViewControllerDelegate.h +// YSLContainerViewControllerDemo +// +// Created by Romy on 6/19/15. +// Copyright © 2015 h.yamaguchi. All rights reserved. +// + +@import UIKit; + +/* + * Delegate so child view controller can communicate with grandparent YSLContainerViewController + */ +@protocol YSLChildViewControllerDelegate + +// Child view controller selects a value, moves it to the appropriate index in the container view +- (void)childViewController:(UIViewController *)childViewController + didChooseIndexValue:(NSInteger)value; + +@end diff --git a/YSLContainerViewController/YSLContainerViewController.h b/YSLContainerViewController/YSLContainerViewController.h index 0b65c5f..85578ad 100644 --- a/YSLContainerViewController/YSLContainerViewController.h +++ b/YSLContainerViewController/YSLContainerViewController.h @@ -7,6 +7,7 @@ // #import +#import "YSLChildViewControllerDelegate.h" @protocol YSLContainerViewControllerDelegate @@ -14,7 +15,7 @@ @end -@interface YSLContainerViewController : UIViewController +@interface YSLContainerViewController : UIViewController @property (nonatomic, weak) id delegate; diff --git a/YSLContainerViewController/YSLContainerViewController.m b/YSLContainerViewController/YSLContainerViewController.m index d05cc78..ba78e5a 100644 --- a/YSLContainerViewController/YSLContainerViewController.m +++ b/YSLContainerViewController/YSLContainerViewController.m @@ -192,4 +192,13 @@ - (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView [self setChildViewControllerWithCurrentIndex:self.currentIndex]; } +#pragma mark - YSLChildViewControllerDelegate + +- (void)childViewController:(UIViewController *)childViewController + didChooseIndexValue:(NSInteger)value { + if (value != self.currentIndex && value < self.childControllers.count) { + [self scrollMenuViewSelectedIndex:value]; + } +} + @end diff --git a/YSLContainerViewControllerDemo.xcodeproj/project.pbxproj b/YSLContainerViewControllerDemo.xcodeproj/project.pbxproj index c0e6677..72cc0e3 100644 --- a/YSLContainerViewControllerDemo.xcodeproj/project.pbxproj +++ b/YSLContainerViewControllerDemo.xcodeproj/project.pbxproj @@ -92,6 +92,7 @@ D0DD9FBF1AFCB67E002CEBB6 /* YSLContainerViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YSLContainerViewController.m; sourceTree = ""; }; D0DD9FC01AFCB67E002CEBB6 /* YSLScrollMenuView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YSLScrollMenuView.h; sourceTree = ""; }; D0DD9FC11AFCB67E002CEBB6 /* YSLScrollMenuView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = YSLScrollMenuView.m; sourceTree = ""; }; + EC861EF71B34BDB0006129DF /* YSLChildViewControllerDelegate.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = YSLChildViewControllerDelegate.h; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -227,6 +228,7 @@ D0DD9FBD1AFCB67E002CEBB6 /* YSLContainerViewController */ = { isa = PBXGroup; children = ( + EC861EF71B34BDB0006129DF /* YSLChildViewControllerDelegate.h */, D0DD9FBE1AFCB67E002CEBB6 /* YSLContainerViewController.h */, D0DD9FBF1AFCB67E002CEBB6 /* YSLContainerViewController.m */, D0DD9FC01AFCB67E002CEBB6 /* YSLScrollMenuView.h */, diff --git a/YSLContainerViewControllerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/YSLContainerViewControllerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..919434a --- /dev/null +++ b/YSLContainerViewControllerDemo.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/YSLContainerViewControllerDemo/ArtistsViewController.h b/YSLContainerViewControllerDemo/ArtistsViewController.h index 97c7d56..5e998f7 100644 --- a/YSLContainerViewControllerDemo/ArtistsViewController.h +++ b/YSLContainerViewControllerDemo/ArtistsViewController.h @@ -7,7 +7,10 @@ // #import +#import "YSLChildViewControllerDelegate.h" @interface ArtistsViewController : UITableViewController +@property (weak, nonatomic) id delegate; + @end diff --git a/YSLContainerViewControllerDemo/ArtistsViewController.m b/YSLContainerViewControllerDemo/ArtistsViewController.m index af011e8..f01a9a2 100644 --- a/YSLContainerViewControllerDemo/ArtistsViewController.m +++ b/YSLContainerViewControllerDemo/ArtistsViewController.m @@ -62,7 +62,8 @@ - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger return _artistsArray.count; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath { +- (UITableViewCell *)tableView:(UITableView *)tableView + scellForRowAtIndexPath:(NSIndexPath *)indexPath { static NSString *cellIdentifier = @"ArtistsCell"; ArtistsCell *cell = (ArtistsCell*)[tableView dequeueReusableCellWithIdentifier:cellIdentifier]; diff --git a/YSLContainerViewControllerDemo/ArtistsViewController.xib b/YSLContainerViewControllerDemo/ArtistsViewController.xib index 877e4d1..0e33cf4 100644 --- a/YSLContainerViewControllerDemo/ArtistsViewController.xib +++ b/YSLContainerViewControllerDemo/ArtistsViewController.xib @@ -1,7 +1,8 @@ - + - + + diff --git a/YSLContainerViewControllerDemo/DetailViewController.h b/YSLContainerViewControllerDemo/DetailViewController.h index ad7be0b..aca0bdd 100644 --- a/YSLContainerViewControllerDemo/DetailViewController.h +++ b/YSLContainerViewControllerDemo/DetailViewController.h @@ -7,7 +7,10 @@ // #import +#import "YSLChildViewControllerDelegate.h" @interface DetailViewController : UIViewController +@property (weak, nonatomic) id delegate; + @end diff --git a/YSLContainerViewControllerDemo/PlayListTableViewController.h b/YSLContainerViewControllerDemo/PlayListTableViewController.h index 85d8972..bd731d3 100644 --- a/YSLContainerViewControllerDemo/PlayListTableViewController.h +++ b/YSLContainerViewControllerDemo/PlayListTableViewController.h @@ -7,7 +7,10 @@ // #import +#import "YSLChildViewControllerDelegate.h" @interface PlayListTableViewController : UITableViewController +@property (weak, nonatomic) id delegate; + @end diff --git a/YSLContainerViewControllerDemo/PlayListTableViewController.xib b/YSLContainerViewControllerDemo/PlayListTableViewController.xib index b1490a2..5c37f2f 100644 --- a/YSLContainerViewControllerDemo/PlayListTableViewController.xib +++ b/YSLContainerViewControllerDemo/PlayListTableViewController.xib @@ -1,7 +1,8 @@ - + - + + diff --git a/YSLContainerViewControllerDemo/SampleViewController.h b/YSLContainerViewControllerDemo/SampleViewController.h index 151664b..8987127 100644 --- a/YSLContainerViewControllerDemo/SampleViewController.h +++ b/YSLContainerViewControllerDemo/SampleViewController.h @@ -7,7 +7,10 @@ // #import +#import "YSLChildViewControllerDelegate.h" @interface SampleViewController : UIViewController +@property (weak, nonatomic) id delegate; + @end diff --git a/YSLContainerViewControllerDemo/SampleViewController.m b/YSLContainerViewControllerDemo/SampleViewController.m index 633f0a5..b19ae78 100644 --- a/YSLContainerViewControllerDemo/SampleViewController.m +++ b/YSLContainerViewControllerDemo/SampleViewController.m @@ -20,11 +20,18 @@ - (void)didReceiveMemoryWarning { - (void)viewWillAppear:(BOOL)animated { - NSLog(@"SampleViewController viewWillAppear"); + [super viewWillAppear:animated]; } - (void)viewDidLoad { [super viewDidLoad]; } +- (IBAction)btnPressed:(id)sender { + if (self.delegate && [self.delegate respondsToSelector:@selector(childViewController:didChooseIndexValue:)]) { + // put it to index one for now + [self.delegate childViewController:self didChooseIndexValue:0]; + } +} + @end diff --git a/YSLContainerViewControllerDemo/SampleViewController.xib b/YSLContainerViewControllerDemo/SampleViewController.xib index ebfafb9..da66c0b 100644 --- a/YSLContainerViewControllerDemo/SampleViewController.xib +++ b/YSLContainerViewControllerDemo/SampleViewController.xib @@ -1,8 +1,8 @@ - + - + diff --git a/YSLContainerViewControllerDemo/ViewController.m b/YSLContainerViewControllerDemo/ViewController.m index 47774e9..b72874d 100644 --- a/YSLContainerViewControllerDemo/ViewController.m +++ b/YSLContainerViewControllerDemo/ViewController.m @@ -60,6 +60,13 @@ - (void)viewDidLoad { containerVC.delegate = self; containerVC.menuItemFont = [UIFont fontWithName:@"Futura-Medium" size:16]; + // delegates for the children + playListVC.delegate = containerVC; + artistVC.delegate = containerVC; + sampleVC1.delegate = containerVC; + sampleVC2.delegate = containerVC; + sampleVC3.delegate = containerVC; + [self.view addSubview:containerVC.view]; }