Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions CodeGenTestApp/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="4514" systemVersion="13B42" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="INF-sx-8CY">
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="5053" systemVersion="13C64" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" initialViewController="INF-sx-8CY">
<dependencies>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3747"/>
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="3733"/>
</dependencies>
<scenes>
<!--Master View Controller-->
Expand Down Expand Up @@ -51,14 +51,24 @@
</collectionView>
<navigationItem key="navigationItem" id="teh-qR-8dZ">
<nil key="title"/>
<barButtonItem key="leftBarButtonItem" title="Perform" id="K89-30-M1C">
<connections>
<action selector="performTapped:" destination="UH6-2k-Soh" id="L5J-Br-hyu"/>
</connections>
</barButtonItem>
<slider key="titleView" opaque="NO" contentMode="scaleToFill" contentHorizontalAlignment="center" contentVerticalAlignment="center" value="120" minValue="0.0" maxValue="160" id="sg9-qZ-59e">
<rect key="frame" x="8" y="8" width="304" height="29"/>
<rect key="frame" x="76" y="8" width="191" height="29"/>
<autoresizingMask key="autoresizingMask" flexibleMaxX="YES" flexibleMaxY="YES"/>
<color key="minimumTrackTintColor" red="0.0" green="0.74117648601531982" blue="0.64313727617263794" alpha="1" colorSpace="deviceRGB"/>
<connections>
<action selector="sliderValueChanged:" destination="UH6-2k-Soh" eventType="valueChanged" id="Tfs-NO-5oy"/>
</connections>
</slider>
<barButtonItem key="rightBarButtonItem" title="Push" id="iCq-LL-Epu">
<connections>
<action selector="pushTapped:" destination="UH6-2k-Soh" id="d84-fn-Y28"/>
</connections>
</barButtonItem>
</navigationItem>
<connections>
<outlet property="cellSizeSlider" destination="sg9-qZ-59e" id="gwU-KP-c6P"/>
Expand All @@ -71,7 +81,7 @@
<!--Detail View Controller - Detail-->
<scene sceneID="Cn3-H9-jdl">
<objects>
<viewController title="Detail" id="Ah7-4n-0Wa" customClass="CGTADetailViewController" sceneMemberID="viewController">
<viewController storyboardIdentifier="Detail View Controller" title="Detail" id="Ah7-4n-0Wa" customClass="CGTADetailViewController" sceneMemberID="viewController">
<layoutGuides>
<viewControllerLayoutGuide type="top" id="EK1-eN-tCq"/>
<viewControllerLayoutGuide type="bottom" id="wQM-DP-Tgk"/>
Expand Down Expand Up @@ -126,4 +136,4 @@
<simulatedOrientationMetrics key="orientation"/>
<simulatedScreenMetrics key="destination" type="retina4"/>
</simulatedMetricsContainer>
</document>
</document>
5 changes: 5 additions & 0 deletions CodeGenTestApp/CGTAMasterViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@
// See the LICENSE file distributed with this work for the terms under
// which Square, Inc. licenses this file to you.

@interface CGTAFlagCollectionViewCell : UICollectionViewCell
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason why you moved this?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In order for CGTAMasterViewController's dequeueImageCellForIndexPath:ofCollectionView: method to return an instance of CGTAFlagCollectionViewCell rather than UICollectionViewCell, it must be able to import the header file which defines that class. By moving the definition to the .h file, it will return this stronger type.


@property (nonatomic, weak) IBOutlet UIImageView *imageView;

@end

@interface CGTAMasterViewController : UICollectionViewController
@end
53 changes: 41 additions & 12 deletions CodeGenTestApp/CGTAMasterViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,13 @@
#import "CGTAImagesCatalog+RuntimeHackery.h"
#import "CGTAMainStoryboardIdentifiers.h"


@interface CGTAFlagCollectionViewCell : UICollectionViewCell

@property (nonatomic, weak) IBOutlet UIImageView *imageView;

@end


@interface CGTAMasterViewController ()

@property (nonatomic, weak) IBOutlet UISlider *cellSizeSlider;
@property (nonatomic, strong) NSArray *flagImages;

@end


@implementation CGTAMasterViewController

#pragma mark - NSObject
Expand All @@ -41,12 +32,43 @@ - (void)awakeFromNib;

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;
{
if ([segue.identifier isEqualToString:CGTAMainStoryboardTapOnFlagIdentifier]) {
// New version: get the properly compiler-checked spelling from the storyboard.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"New version" doesn't have any meaning here. To somebody looking this code for an example, this comment comparing the current version of the library to a former version of the library isn't helpful.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed the comment completely.

// ... here we are guaranteed that tapOnFlag is one of our own view controller's segue and not some random one in the storyboard
if ([segue.identifier isEqualToString:[self tapOnFlagSegueIdentifier]]) {
CGTADetailViewController *detailViewController = segue.destinationViewController;
detailViewController.image = ((CGTAFlagCollectionViewCell *)sender).imageView.image;
detailViewController.image = ((CGTAFlagCollectionViewCell *)sender).imageView.image ?: [CGTAImagesCatalog usaImage];
}
}

- (IBAction)pushTapped:(id)sender
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate that you're trying to illustrate something here, but to have a bar button item that does the same thing as the segue in the file is confusing. If you want to show different functionality, it should probably be something that you can't do via storyboard-defined segue.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the two buttons and their actions. If I think of a better way to show off this functionality, I will add it to a future pull request.

{
CGTADetailViewController *detailViewController = nil;

// Initial version: full of strings that you have to type correctly!
// Misspell any of these and your app will not work as expected.
UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
detailViewController = [storyboard instantiateViewControllerWithIdentifier:@"Detail View Controller"];

// New version: the two lines are combined into one ensuring that "Detail View Controller" does indeed belong to the "Main" storyboard
detailViewController = [CGTAMainStoryboard instantiateDetailViewController];

// ... also notice how this returns a CGTADetailViewController, rather than an id, so we can be assured that .image is a valid property!
detailViewController.image = [CGTAImagesCatalog usaImage];
[self.navigationController pushViewController:detailViewController animated:YES];
}

- (IBAction)performTapped:(id)sender
{
// Initial version: uses a string that you have to type correctly!
// Misspell this and your app will not work as expected.
#if 0
[self performSegueWithIdentifier:@"Tap on Flag" sender:nil];
#endif

// New version: get the properly compiler-checked spelling from the storyboard.
[self performTapOnFlagSegue];
}

#pragma mark - Private methods

- (IBAction)sliderValueChanged:(UISlider *)sender;
Expand Down Expand Up @@ -85,7 +107,14 @@ - (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSe

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath;
{
CGTAFlagCollectionViewCell *cell = (CGTAFlagCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:CGTAMainStoryboardImageCellIdentifier forIndexPath:indexPath];
CGTAFlagCollectionViewCell *cell = nil;

// Initial version: we must type in the identifier, and have no guarantees as to which class it returns
cell = (CGTAFlagCollectionViewCell *)[collectionView dequeueReusableCellWithReuseIdentifier:@"Image Cell" forIndexPath:indexPath];

// New version: class extension which returns the exact type we are expecting
cell = [self dequeueImageCellForIndexPath:indexPath ofCollectionView:collectionView];

cell.imageView.image = self.flagImages[indexPath.item];
return cell;
}
Expand Down
10 changes: 10 additions & 0 deletions CodeGenTestApp/CodeGenTestApp.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
A881854118A9B622002803FC /* CGTAImagesCatalog+RuntimeHackery.m in Sources */ = {isa = PBXBuildFile; fileRef = A838793518A05B6D00B386D6 /* CGTAImagesCatalog+RuntimeHackery.m */; };
A881854218A9B622002803FC /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = A83878E518A0367C00B386D6 /* main.m */; };
A881854418A9B663002803FC /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = A83878EB18A0367C00B386D6 /* Main.storyboard */; };
AA24EC5A18EB4F8E00DB0F94 /* MoreExamples.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = AA24EC5918EB4F8E00DB0F94 /* MoreExamples.storyboard */; };
AAA9F41518ED1A0C00BA7A27 /* CGTAMoreExamplesStoryboardIdentifiers.m in Sources */ = {isa = PBXBuildFile; fileRef = AAA9F41418ED1A0C00BA7A27 /* CGTAMoreExamplesStoryboardIdentifiers.m */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -95,6 +97,9 @@
A881852518A9B512002803FC /* UIKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = UIKit.framework; path = Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS7.0.sdk/System/Library/Frameworks/UIKit.framework; sourceTree = DEVELOPER_DIR; };
A881852718A9B520002803FC /* codegenutils.xcodeproj */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.pb-project"; name = codegenutils.xcodeproj; path = ../codegenutils.xcodeproj; sourceTree = "<group>"; };
A89D8FE617CFFDCE0077F2B5 /* Foundation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Foundation.framework; path = System/Library/Frameworks/Foundation.framework; sourceTree = SDKROOT; };
AA24EC5918EB4F8E00DB0F94 /* MoreExamples.storyboard */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = file.storyboard; path = MoreExamples.storyboard; sourceTree = "<group>"; };
AAA9F41318ED1A0C00BA7A27 /* CGTAMoreExamplesStoryboardIdentifiers.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CGTAMoreExamplesStoryboardIdentifiers.h; sourceTree = "<group>"; };
AAA9F41418ED1A0C00BA7A27 /* CGTAMoreExamplesStoryboardIdentifiers.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CGTAMoreExamplesStoryboardIdentifiers.m; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand All @@ -117,6 +122,7 @@
A83878E818A0367C00B386D6 /* CGTAAppDelegate.h */,
A83878E918A0367C00B386D6 /* CGTAAppDelegate.m */,
A83878EB18A0367C00B386D6 /* Main.storyboard */,
AA24EC5918EB4F8E00DB0F94 /* MoreExamples.storyboard */,
A83878F418A0367C00B386D6 /* Images.xcassets */,
A83878EE18A0367C00B386D6 /* CGTAMasterViewController.h */,
A83878EF18A0367C00B386D6 /* CGTAMasterViewController.m */,
Expand Down Expand Up @@ -146,6 +152,8 @@
A838791518A0455E00B386D6 /* CGTAImagesCatalog.m */,
A838793118A0557E00B386D6 /* CGTAMainStoryboardIdentifiers.h */,
A838793218A0557E00B386D6 /* CGTAMainStoryboardIdentifiers.m */,
AAA9F41318ED1A0C00BA7A27 /* CGTAMoreExamplesStoryboardIdentifiers.h */,
AAA9F41418ED1A0C00BA7A27 /* CGTAMoreExamplesStoryboardIdentifiers.m */,
A838791818A04AB300B386D6 /* CGTATestAppColorList.h */,
A838791918A04AB300B386D6 /* CGTATestAppColorList.m */,
A838793418A05B6D00B386D6 /* CGTAImagesCatalog+RuntimeHackery.h */,
Expand Down Expand Up @@ -280,6 +288,7 @@
buildActionMask = 2147483647;
files = (
A881854418A9B663002803FC /* Main.storyboard in Resources */,
AA24EC5A18EB4F8E00DB0F94 /* MoreExamples.storyboard in Resources */,
A881853A18A9B614002803FC /* Images.xcassets in Resources */,
A83878E418A0367C00B386D6 /* InfoPlist.strings in Resources */,
);
Expand Down Expand Up @@ -309,6 +318,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
AAA9F41518ED1A0C00BA7A27 /* CGTAMoreExamplesStoryboardIdentifiers.m in Sources */,
A881853918A9B609002803FC /* CGTAAppDelegate.m in Sources */,
A881853C18A9B622002803FC /* CGTAMasterViewController.m in Sources */,
A881853F18A9B622002803FC /* CGTAMainStoryboardIdentifiers.m in Sources */,
Expand Down
Loading