1

我似乎在我的应用程序中设置我的委托时遇到了一些困难。我认为这部分是由于它是一个 UIPageView。

在我的 DetailPage (UIPageViewController)

#import "PhotoViewController.h"

@interface DetailPageController : UIPageViewController <UIPageViewControllerDelegate, PhotoViewControllerDelegate>

@property (nonatomic, strong) PhotoViewController *photoViewController;


@end

在.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

我正在根据模式设置页面视图,我可以设置视图控制器并且它显示得很好。但我不知道在哪里设置我的代表?我从不在任何地方分配视图控制器(PhotoViewController)。

模态控制器.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];
}

然后实际的页面视图代码如下:

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

米:

#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

callHome 方法在按下按钮时被调用,我只是想找到在哪里设置这个委托。

我看到它的方式应该是这样的:

Button Pressed -> callingHome 被调用 callHome 被调用 -> _photoViewControllerDelegate 在我的主 UIPageView 控制器中调用 photoCall 方法

它记录到控制台,我们完成了!

4

1 回答 1

2

替换这些行:

UIViewController *initialController = (UIViewController *)[self.modelController viewControllerAtIndex:self.initialIndex storyboard:self.storyboard];
self.delegate = self;

PhotoViewController *initialController = [self.modelController viewControllerAtIndex:self.initialIndex storyboard:self.storyboard];
initialController.delegate = self;

在您的应用程序的其余部分中,每当您实例化PhotoViewController要由DetailPageController实例管理的 a 时,请确保将页面控制器设置为照片视图控制器的委托。例如,更改您的UIPageViewControllerDataSource方法,如下所示:

- (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;
}

或者修改您的viewControllerAtIndex:storyboard:方法以获取委托参数并在那里进行分配。但是对您来说很方便 - 只要确保您在创建这些代表时分配这些代表以进入您的页面。

于 2013-08-13T02:36:34.727 回答