1

我是一个相对较新的 iPhone 开发人员,并且在构建我的第二个 iPhone 应用程序方面取得了很大进展。在我现在正在构建的应用程序中,我正在使用一些协议和委托进行一些代码分离,以便我在整个代码的各个地方重用我的一些代码。

这就是我想要发生的事情:

  1. CITRootViewController 创建 CITReportCreator 类的实例,将自身作为属性传递,以便 reportCreator 可以打开其他视图控制器等。
  2. CITReportCreator 类被声明为实现 CITImageCaptureDelegate 协议,该协议在 CITImageCaptureViewController 文件中声明。
  3. CITImageCaptureViewController 定义了委托协议,并有一个方法将数据和引用传回给子视图控制器,以便 CITReportCreator 可以与其数据交互,关闭相关的 XIB 等。

我相信我正确建立了委托和协议,并验证了我的“委托”对象在被调用时仍然包含数据,但是当我的视图控制器尝试将数据传递回委托时,我得到了一个 EXC_BAD_ACCESS 方法这行代码:

   [self.delegate childViewControllerDidFinish:self];

这是我其余代码的很大一部分。我通过使用 CITRootViewController 作为我的委托而不是 CITReportCreator 类来完成这项工作,但是现在我正在分离代码,有些东西已经坏了。

CITReootViewController.m(调用 Report Creator 的视图控制器)

//create a nrew report
-(IBAction)createReport:(id)sender {
    CITReportCreator *report = [CITReportCreator alloc];
    [report createNewReport:self];
}

CITReportCreator.h

#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import "CITImageCaptureViewController.h"

@interface CITReportCreator : NSObject <CITImageCaptureDelegate>

@property (strong, nonatomic) NSArray *imageList;
@property (nonatomic) NSInteger imageIndex;


-(int) createNewReport:(UIViewController *)parent ;

//Delegate Methods
-(void) childViewControllerDidFinish:(UIViewController*)viewController;
@end

和 CITReportCreator.m

#import "CITReportCreator.h"

@implementation CITReportCreator
{
    UIViewController *parentController;
}

@synthesize  imageList;
@synthesize imageIndex;

-(int) createNewReport:(UIViewController *)parent
{

//store a reference to the parent view controller
parentController = parent;

// init code....

//head to the first image capture view
[self  startImageCapture];
return 0;
}  

-(int)startImageCapture
{
//pull the image name from the array of images
NSString *imageName = [imageList objectAtIndex:imageIndex];

//prep the image capture controller
CITImageCaptureViewController *capture = [[CITImageCaptureViewController alloc] initWithNibName:@"CITImageCaptureViewController" bundle:nil];

//Assign the capture controller's delegate
capture.imageName = imageName;
capture.delegate = self;

//Display the capture controller
[parentController presentModalViewController:capture animated:YES];

return 0;
}


//a break point set here never gets hit.
-(void) childViewControllerDidFinish:(UIViewController*)viewController;
{
[viewController dismissModalViewControllerAnimated:YES]; 
}

@end

最后,CITImageCaptureViewControllers

CITImageCaptureViewController.h

#import <UIKit/UIKit.h>
@protocol CITImageCaptureDelegate <NSObject>
-(void) childViewControllerDidFinish:(UIViewController*)viewController;
@end


@interface CITImageCaptureViewController : UIViewController 
{
id<CITImageCaptureDelegate> delegate;
}

@property (nonatomic,assign) id<CITImageCaptureDelegate> delegate;
@property (nonatomic, assign) NSString *imageName;

//continue button pressed method
-(IBAction)continueButtonPressed:(id)sender;

@end

和 .m 文件

#import "CITImageCaptureViewController.h"

@interface CITImageCaptureViewController ()
@end

@implementation CITImageCaptureViewController
@synthesize navItem;

@synthesize  imageName;
@synthesize  delegate = _delegate; //i think this may be part of the problem

//cutting out initWithNibName, viewDidLoad, etc...



- (IBAction)continueButtonPressed:(id)sender
{
[self.delegate childViewControllerDidFinish:self];
} 
@end

我发现委托和协议没有那么简单,但我猜我在某处遗漏了一个小改动。你能帮助我朝着正确的方向前进吗?

4

0 回答 0