From ab267f306dee3ef5d4e134068dabb71cd135bb99 Mon Sep 17 00:00:00 2001 From: Yuriy Sannikov Date: Sun, 23 Nov 2014 10:45:41 -0500 Subject: [PATCH 1/2] Add ability to use different storyboards and bundles --- AnotherStoryboard.storyboard | 43 +++++++++++++++++++ .../project.pbxproj | 4 ++ .../Example/MSPageViewControllerExample.m | 2 +- .../MSPageViewControllerExample.storyboard | 11 ++--- .../Source/MSPageViewController.m | 27 +++++++++++- README.md | 8 +++- 6 files changed, 85 insertions(+), 10 deletions(-) create mode 100644 AnotherStoryboard.storyboard diff --git a/AnotherStoryboard.storyboard b/AnotherStoryboard.storyboard new file mode 100644 index 0000000..2f7c9e1 --- /dev/null +++ b/AnotherStoryboard.storyboard @@ -0,0 +1,43 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/MSPageViewController.xcodeproj/project.pbxproj b/MSPageViewController.xcodeproj/project.pbxproj index 37968c3..f05911c 100644 --- a/MSPageViewController.xcodeproj/project.pbxproj +++ b/MSPageViewController.xcodeproj/project.pbxproj @@ -23,6 +23,7 @@ 57EFFA8A18B159B5002E5577 /* MSPageViewControllerPage.m in Sources */ = {isa = PBXBuildFile; fileRef = 57EFFA8918B159B5002E5577 /* MSPageViewControllerPage.m */; }; 57EFFA8E18B15AC6002E5577 /* MSPageViewControllerExample.m in Sources */ = {isa = PBXBuildFile; fileRef = 57EFFA8D18B15AC6002E5577 /* MSPageViewControllerExample.m */; }; 57EFFA9018B15AD9002E5577 /* MSPageViewControllerExample.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 57EFFA8F18B15AD9002E5577 /* MSPageViewControllerExample.storyboard */; }; + 653D9C051A22362E00DCE4F5 /* AnotherStoryboard.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 653D9C041A22362E00DCE4F5 /* AnotherStoryboard.storyboard */; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -60,6 +61,7 @@ 57EFFA8C18B15AC6002E5577 /* MSPageViewControllerExample.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = MSPageViewControllerExample.h; path = Example/MSPageViewControllerExample.h; sourceTree = ""; }; 57EFFA8D18B15AC6002E5577 /* MSPageViewControllerExample.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; name = MSPageViewControllerExample.m; path = Example/MSPageViewControllerExample.m; sourceTree = ""; }; 57EFFA8F18B15AD9002E5577 /* MSPageViewControllerExample.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = MSPageViewControllerExample.storyboard; path = Example/MSPageViewControllerExample.storyboard; sourceTree = ""; }; + 653D9C041A22362E00DCE4F5 /* AnotherStoryboard.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; name = AnotherStoryboard.storyboard; path = ../AnotherStoryboard.storyboard; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -176,6 +178,7 @@ 57EFFA8C18B15AC6002E5577 /* MSPageViewControllerExample.h */, 57EFFA8D18B15AC6002E5577 /* MSPageViewControllerExample.m */, 57EFFA8F18B15AD9002E5577 /* MSPageViewControllerExample.storyboard */, + 653D9C041A22362E00DCE4F5 /* AnotherStoryboard.storyboard */, ); name = Example; sourceTree = ""; @@ -257,6 +260,7 @@ buildActionMask = 2147483647; files = ( 57EFFA9018B15AD9002E5577 /* MSPageViewControllerExample.storyboard in Resources */, + 653D9C051A22362E00DCE4F5 /* AnotherStoryboard.storyboard in Resources */, 57EFFA5C18B149F7002E5577 /* InfoPlist.strings in Resources */, 57EFFA6418B149F7002E5577 /* Images.xcassets in Resources */, ); diff --git a/MSPageViewController/Example/MSPageViewControllerExample.m b/MSPageViewController/Example/MSPageViewControllerExample.m index d197229..9a5550b 100644 --- a/MSPageViewController/Example/MSPageViewControllerExample.m +++ b/MSPageViewController/Example/MSPageViewControllerExample.m @@ -19,7 +19,7 @@ + (void)initialize { } - (NSArray *)pageIdentifiers { - return @[@"page1", @"page2", @"page3", @"page4"]; + return @[@"page1", @"page2", @"page3", @"page4", @"AnotherStoryboard:page5"]; } @end diff --git a/MSPageViewController/Example/MSPageViewControllerExample.storyboard b/MSPageViewController/Example/MSPageViewControllerExample.storyboard index f38f3eb..9ca0cc2 100644 --- a/MSPageViewController/Example/MSPageViewControllerExample.storyboard +++ b/MSPageViewController/Example/MSPageViewControllerExample.storyboard @@ -1,7 +1,8 @@ - + - + + @@ -18,7 +19,6 @@ \ No newline at end of file + diff --git a/MSPageViewController/Source/MSPageViewController.m b/MSPageViewController/Source/MSPageViewController.m index 880af8b..29bf942 100644 --- a/MSPageViewController/Source/MSPageViewController.m +++ b/MSPageViewController/Source/MSPageViewController.m @@ -92,7 +92,7 @@ - (UIViewController *)viewControllerAtIndex:(NSInteger)index { NSAssert(self.storyboard, @"This controller is only meant to be used inside of a UIStoryboard"); - result = [self.storyboard instantiateViewControllerWithIdentifier:self.pageIdentifiers[(NSUInteger)index]]; + result = [self instantiateViewController: self.pageIdentifiers[(NSUInteger)index] withIndex:index]; NSParameterAssert(result); NSAssert([result conformsToProtocol:@protocol(MSPageViewControllerChild)], @@ -119,4 +119,29 @@ - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)page return [pageViewController.viewControllers.lastObject pageIndex]; } +#pragma mark ViewController instantiation +-(UIViewController *)instantiateViewController:(NSString*)identifier withIndex: (NSInteger) index { + UIViewController *result = nil; + + UIStoryboard* storyboard = self.storyboard; + + NSArray* storyboardAndIdentifier = [identifier componentsSeparatedByString: @":"]; + + if (storyboardAndIdentifier.count == 2) { + storyboard = [UIStoryboard storyboardWithName: storyboardAndIdentifier[0] bundle:[self storyboardBundleForIndex: index]]; + identifier = storyboardAndIdentifier[1]; + NSAssert(storyboard, @"Unable to find specified Storyboard by name"); + NSAssert([identifier length] > 0, @"Specified identified should not be empty"); + } + result = [storyboard instantiateViewControllerWithIdentifier:identifier]; + + NSParameterAssert(result); + return result; +} + +- (NSBundle*)storyboardBundleForIndex:(NSInteger)index { + return nil; +} + + @end diff --git a/README.md b/README.md index 5bd79fc..03799a1 100644 --- a/README.md +++ b/README.md @@ -11,7 +11,7 @@ With ```MSPageViewController``` you will be able to design each page from a sing First you must create a [subclass of ```MSPageViewController```](MSPageViewController/Source/MSPageViewController+Protected.h) and override ```-pageIdentifiers```. Example: ```objc - (NSArray *)pageIdentifiers { - return @[@"page1", @"page2"]; + return @[@"page1", @"page2", @"AnotherStoryboard:page3"]; } ``` @@ -19,6 +19,12 @@ Then you have to create a storyboard, add a ```UIPageViewController``` object an Then you can add the controllers, setting their ```Storyboard ID```s to what you returned in ```pageIdentifiers```. Each of them must be a class that conforms to ```MSPageViewControllerChild``` (if you don't need to add any extra functionality to it you can use [```MSPageViewControllerPage```](MSPageViewController/Source/MSPageViewControllerPage.h)). +You might specify Storyboard name using colon +``` +StoryBoardName:pageIdentifier +``` +Also, you can specify bunlde. In order to do that you have to override ```- (NSBundle*)storyboardBundleForIndex:(NSInteger)index``` + When your controller is instantiated, it will use these controllers to create each page. Make sure you also check out the sample project in this repo. From 7a91d86ef0c3d3fb941d09e5e5f00e6fd839c1a9 Mon Sep 17 00:00:00 2001 From: Yuriy Sannikov Date: Tue, 25 Nov 2014 20:31:20 -0500 Subject: [PATCH 2/2] Allow override sotryboard and controller factories --- .../AppIcon.appiconset/Contents.json | 5 +++++ .../Source/MSPageViewController+Protected.h | 13 +++++++++++++ .../Source/MSPageViewController.m | 16 ++++++++++------ 3 files changed, 28 insertions(+), 6 deletions(-) diff --git a/MSPageViewController/Images.xcassets/AppIcon.appiconset/Contents.json b/MSPageViewController/Images.xcassets/AppIcon.appiconset/Contents.json index a396706..33ec0bc 100644 --- a/MSPageViewController/Images.xcassets/AppIcon.appiconset/Contents.json +++ b/MSPageViewController/Images.xcassets/AppIcon.appiconset/Contents.json @@ -14,6 +14,11 @@ "idiom" : "iphone", "size" : "60x60", "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" } ], "info" : { diff --git a/MSPageViewController/Source/MSPageViewController+Protected.h b/MSPageViewController/Source/MSPageViewController+Protected.h index 4f1e1a4..0832866 100644 --- a/MSPageViewController/Source/MSPageViewController+Protected.h +++ b/MSPageViewController/Source/MSPageViewController+Protected.h @@ -34,4 +34,17 @@ */ - (UIViewController *)viewControllerAtIndex:(NSInteger)index; +/** + * @note Can be overridden. + * @discussion Storyboard factory. May be overriden to provide TyphoonStoryboard instance + */ +- (UIStoryboard *)storyboardWithName:(NSString *)name index:(NSInteger) storyboardIndex; + +/** + * @note Can be overridden. + * @discussion Controller factory. + */ + +- (UIViewController *)instantiateViewControllerWithIdentifier:(NSString*)identifier storyboard:(UIStoryboard*) fromStoryboard; + @end diff --git a/MSPageViewController/Source/MSPageViewController.m b/MSPageViewController/Source/MSPageViewController.m index 29bf942..d8462e2 100644 --- a/MSPageViewController/Source/MSPageViewController.m +++ b/MSPageViewController/Source/MSPageViewController.m @@ -52,6 +52,14 @@ - (void)setUpViewController:(UIViewController *)viewC } +- (UIStoryboard *)storyboardWithName:(NSString *)name index:(NSInteger) storyboardIndex { + return [UIStoryboard storyboardWithName: name bundle: nil]; +} + +- (UIViewController *)instantiateViewControllerWithIdentifier:(NSString*)identifier storyboard:(UIStoryboard*) fromStoryboard { + return [fromStoryboard instantiateViewControllerWithIdentifier:identifier]; +} + #pragma mark - - (void)viewDidLoad { @@ -128,20 +136,16 @@ - (NSInteger)presentationIndexForPageViewController:(UIPageViewController *)page NSArray* storyboardAndIdentifier = [identifier componentsSeparatedByString: @":"]; if (storyboardAndIdentifier.count == 2) { - storyboard = [UIStoryboard storyboardWithName: storyboardAndIdentifier[0] bundle:[self storyboardBundleForIndex: index]]; + storyboard = [self storyboardWithName: storyboardAndIdentifier[0] index: index]; identifier = storyboardAndIdentifier[1]; NSAssert(storyboard, @"Unable to find specified Storyboard by name"); NSAssert([identifier length] > 0, @"Specified identified should not be empty"); } - result = [storyboard instantiateViewControllerWithIdentifier:identifier]; + result = (UIViewController*)[self instantiateViewControllerWithIdentifier:identifier storyboard: storyboard]; NSParameterAssert(result); return result; } -- (NSBundle*)storyboardBundleForIndex:(NSInteger)index { - return nil; -} - @end