-
Notifications
You must be signed in to change notification settings - Fork 26
Adds class methods and strongly typed return values. #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 2 commits
880afd9
cd43136
795d560
85d9a15
eeed36e
7f12289
987c621
61fb2e5
bc442e3
60bd373
79a0a60
e78ed89
5b7c86c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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. | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
|
|
@@ -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; | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.