我似乎在我的应用中设置我的委托有些困难。我认为这部分归因于它是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)。
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];
}
然后实际页面视图的代码如下:
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
按下按钮调用callHome方法,我只想找到设置此委托的位置。
我看到它的方式应该是这样的:
按下按钮 - &gt; callHome被调用 callHome被称为 - &gt; _photoViewControllerDelegate在我的主UIPageView控制器中调用方法photoCall
它登录到控制台,我们完成了!
答案 0 :(得分: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
时,请确保将页面控制器设置为照片视图控制器的代理。例如,如图所示更改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:
方法以获取委托参数并在那里进行分配。但是对您来说很方便 - 只需确保在创建这些代表时分配这些代表以进入您的页面。