gpt4 book ai didi

ios - 正确设置委托(delegate)

转载 作者:行者123 更新时间:2023-11-29 10:56:18 24 4
gpt4 key购买 nike

我似乎在我的应用程序中设置委托(delegate)时遇到了一些困难。我认为这部分是因为它是一个 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

我正在设置基于模态的页面 View ,我可以设置 View Controller 并且它显示正常。但我不知道在哪里设置我的委托(delegate)?我从不在任何地方分配 View Controller (PhotoViewController)。

模态 Controller .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];
}

那么实际的page view有如下代码:

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

callingHome 方法在按下按钮时被调用,我只是想找到设置此委托(delegate)的位置。

我认为它应该像这样工作:

按下按钮 -> callingHome 被调用调用 callingHome -> _photoViewControllerDelegate 在我的主 UIPageView Controller 中调用方法 photoCall

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

最佳答案

替换这些行:

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 实例管理时,确保将页面 Controller 设置为照片 View Controller 。例如,更改您的 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: 方法以获取委托(delegate)参数并在那里进行分配。然而对您来说很方便 - 只要确保在您创建这些委托(delegate)时分配这些委托(delegate)以进入您的页面即可。

关于ios - 正确设置委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18199456/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com