Reputation: 1351
I seem to be having some difficulty setting my delegate in my app. I think this is partially due the it being a UIPageView.
In my DetailPage (the UIPageViewController)
#import "PhotoViewController.h"
@interface DetailPageController : UIPageViewController <UIPageViewControllerDelegate, PhotoViewControllerDelegate>
@property (nonatomic, strong) PhotoViewController *photoViewController;
@end
And in the .m
#import "DetailPageController.h"
#import "DetailPageModelController.h"
@interface DetailPageController ()
@end
- (void)viewDidLoad
{
[super viewDidLoad];
self.modelController.photos = self.photos;
UIViewController *initialController = (UIViewController *)[self.modelController viewControllerAtIndex:self.initialIndex storyboard:self.storyboard];
self.delegate = self;
[self setViewControllers:@[initialController]
direction:UIPageViewControllerNavigationDirectionForward
animated:NO
completion:^(BOOL finished) {
}];
}
- (void)photoCall
{
NSLog(@"called from photoCall");
}
@end
I am setting the page view based off a modal, I can set the view controller and it displays fine. But I don't know where to set my delegate? I never allocate the view controller (PhotoViewController) anywhere.
ModalController.m:
#import "DetailPageModelController.h"
#import "PhotoViewController.h"
#import "Media.h"
@implementation DetailPageModelController
- (UIImage *)photoAtIndex:(NSUInteger)index {
if ([self.photos count] == 0 || index >=[self.photos count]) {
return nil;
}
Media *object = [[Media alloc] init];
object = self.photos[index];
ALAssetRepresentation *rep = [object.asset defaultRepresentation];
CGImageRef iref = [rep fullResolutionImage];
UIImage *image = [UIImage imageWithCGImage:iref];
return image;
}
#pragma mark - convenience methods
- (PhotoViewController *)viewControllerAtIndex:(NSUInteger)index storyboard:(UIStoryboard *)storyboard
{
UIImage *photo = [self photoAtIndex:index];
if (photo == nil) {
return nil;
}
//No segue, load manually
PhotoViewController *controller = [storyboard instantiateViewControllerWithIdentifier:@"PhotoViewController"];
controller.photo = photo;
controller.num = index;
return controller;
}
- (NSUInteger)indexOfViewController:(PhotoViewController *)controller
{
//Returns the position in array of photos that current picture is
//Same as current view controller
return controller.num;
}
#pragma mark - page view data source
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(PhotoViewController *) viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(PhotoViewController *) viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.photos count]) {
return nil;
}
return [self viewControllerAtIndex:index storyboard:viewController.storyboard];
}
Then the actual page view has code as following:
PhotoViewController.h
@protocol PhotoViewControllerDelegate <NSObject>
- (void)photoCall;
@end
@interface PhotoViewController : UIViewController
@property (nonatomic, strong) UIImage *photo;
//storyboard
@property (nonatomic, strong) IBOutlet UIImageView *imageView;
@property (nonatomic, strong) id<PhotoViewControllerDelegate> photoViewControllerDelegate;
@end
.m:
#import "PhotoViewController.h"
@interface PhotoViewController ()
@end
@implementation PhotoViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
[self setPhotoView];
}
- (void)setPhotoView
{
[self.imageView setImage: _photo];
self.imageView.frame = [[UIScreen mainScreen] bounds];
}
- (void)callingHome
{
NSLog(@"tcd called");
[_photoViewControllerDelegate photoCall];
}
@end
The method callingHome is called on a button press, i'm just trying to find where to set this delegate.
The way I see it is it should work like this:
Button Pressed -> callingHome is called callingHome is called -> _photoViewControllerDelegate calls the method photoCall in my main UIPageView Controller
It logs to console and we are done!
Upvotes: 1
Views: 962
Reputation: 18363
Replace these lines:
UIViewController *initialController = (UIViewController *)[self.modelController viewControllerAtIndex:self.initialIndex storyboard:self.storyboard];
self.delegate = self;
With
PhotoViewController *initialController = [self.modelController viewControllerAtIndex:self.initialIndex storyboard:self.storyboard];
initialController.delegate = self;
And in the rest of your application, whenever you instantiate a PhotoViewController
to be managed by a DetailPageController
instance, make sure to set the page controller as the delegate of the photo view controller. For example, change your UIPageViewControllerDataSource
methods as shown:
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(PhotoViewController *) viewController];
if ((index == 0) || (index == NSNotFound)) {
return nil;
}
index--;
PhotoViewController *photoVC = [self viewControllerAtIndex:index storyboard:viewController.storyboard];
if ([pageViewController conformsToProtocol:@protocol(PhotoViewControllerDelegate)]) {
photoVC.delegate = (id<PhotoViewControllerDelegate>)pageViewController;
}
return photoVC;
}
- (UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(UIViewController *)viewController
{
NSUInteger index = [self indexOfViewController:(PhotoViewController *) viewController];
if (index == NSNotFound) {
return nil;
}
index++;
if (index == [self.photos count]) {
return nil;
}
PhotoViewController *photoVC = [self viewControllerAtIndex:index storyboard:viewController.storyboard];
if ([pageViewController conformsToProtocol:@protocol(PhotoViewControllerDelegate)]) {
photoVC.delegate = (id<PhotoViewControllerDelegate>)pageViewController;
}
return photoVC;
}
Or modify your viewControllerAtIndex:storyboard:
method to take a delegate parameter and do the assignment there. However is convenient for you - just make sure you assign those delegates whenever you're creating them to go into your pages.
Upvotes: 2