Skip to content

Commit

Permalink
Add delegate to the designated initializer
Browse files Browse the repository at this point in the history
closes nytimes#95
  • Loading branch information
cdzombak committed Feb 1, 2016
1 parent 81e4b4c commit b04b273
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 6 deletions.
15 changes: 13 additions & 2 deletions Pod/Classes/ios/NYTPhotosViewController.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,23 +89,34 @@ extern NSString * const NYTPhotosViewControllerDidDismissNotification;
@property (nonatomic, weak, nullable) id <NYTPhotosViewControllerDelegate> delegate;

/**
* A convenience initializer that calls `initWithPhotos:initialPhoto:`, passing the first photo as the `initialPhoto` argument.
* A convenience initializer that calls `initWithPhotos:initialPhoto:delegate:`, passing the first photo as the `initialPhoto` argument, and `nil` as the `delegate` argument.
*
* @param photos An array of objects conforming to the `NYTPhoto` protocol.
*
* @return A fully initialized object.
*/
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos;

/**
* A convenience initializer that calls `initWithPhotos:initialPhoto:delegate:`, passing `nil` as the `delegate` argument.
*
* @param photos An array of objects conforming to the `NYTPhoto` protocol.
* @param initialPhoto The photo to display initially. Must be contained within the `photos` array. If `nil` or not within the `photos` array, the first photo within the `photos` array will be displayed.
*
* @return A fully initialized object.
*/
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos initialPhoto:(id <NYTPhoto> _Nullable)initialPhoto;

/**
* The designated initializer that stores the array of objects conforming to the `NYTPhoto` protocol for display, along with specifying an initial photo for display.
*
* @param photos An array of objects conforming to the `NYTPhoto` protocol.
* @param initialPhoto The photo to display initially. Must be contained within the `photos` array. If `nil` or not within the `photos` array, the first photo within the `photos` array will be displayed.
* @param delegate The delegate for this `NYTPhotosViewController`.
*
* @return A fully initialized object.
*/
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos initialPhoto:(id <NYTPhoto> _Nullable)initialPhoto NS_DESIGNATED_INITIALIZER;
- (instancetype)initWithPhotos:(NSArray <id <NYTPhoto>> * _Nullable)photos initialPhoto:(id <NYTPhoto> _Nullable)initialPhoto delegate:(nullable id <NYTPhotosViewControllerDelegate>)delegate NS_DESIGNATED_INITIALIZER;

/**
* Displays the specified photo. Can be called before the view controller is displayed. Calling with a photo not contained within the data source has no effect.
Expand Down
14 changes: 10 additions & 4 deletions Pod/Classes/ios/NYTPhotosViewController.m
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ - (instancetype)initWithCoder:(NSCoder *)aDecoder {
self = [super initWithCoder:aDecoder];

if (self) {
[self commonInitWithPhotos:nil initialPhoto:nil];
[self commonInitWithPhotos:nil initialPhoto:nil delegate:nil];
}

return self;
Expand Down Expand Up @@ -156,21 +156,27 @@ - (void)dismissViewControllerAnimated:(BOOL)animated completion:(void (^)(void))
#pragma mark - NYTPhotosViewController

- (instancetype)initWithPhotos:(NSArray *)photos {
return [self initWithPhotos:photos initialPhoto:photos.firstObject];
return [self initWithPhotos:photos initialPhoto:photos.firstObject delegate:nil];
}

- (instancetype)initWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto {
return [self initWithPhotos:photos initialPhoto:initialPhoto delegate:nil];
}

- (instancetype)initWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto delegate:(id<NYTPhotosViewControllerDelegate>)delegate {
self = [super initWithNibName:nil bundle:nil];

if (self) {
[self commonInitWithPhotos:photos initialPhoto:initialPhoto];
[self commonInitWithPhotos:photos initialPhoto:initialPhoto delegate:delegate];
}

return self;
}

- (void)commonInitWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto {
- (void)commonInitWithPhotos:(NSArray *)photos initialPhoto:(id <NYTPhoto>)initialPhoto delegate:(id<NYTPhotosViewControllerDelegate>)delegate {
_dataSource = [[NYTPhotosDataSource alloc] initWithPhotos:photos];
_delegate = delegate;

_panGestureRecognizer = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(didPanWithGestureRecognizer:)];
_singleTapGestureRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didSingleTapWithGestureRecognizer:)];

Expand Down

0 comments on commit b04b273

Please sign in to comment.