如何在视图控制器之间导航时解决错误(与内存相关)

时间:2014-08-22 14:17:24

标签: ios memory-management uinavigationcontroller nsarray nsobject

我有两个视图控制器(CreateReport& ViewReport)。和一个NSObject类(PDFRenderer)。

infoArray(NSMutableArray)在PDFRenderer(NSObject类)&中被声明为extern。在CreateReport类中通过import#PDFRenderer.h访问。用户选择所需的tableview行并将所选项传递给infoArray(NSMutableArray)并在导航堆栈上推送ViewReport类以查看使用数组中的项生成的pdf。

CREATEREPORT - (PDFRenderer) - > ViewReport

问题:在第一次查看pdf之后没有问题 - 现在我想从ViewReport导航回CreateReport - 选择或取消选择所需的表格单元格 - 更新infoArray - 并推送ViewReport类以查看使用更新的infoArray中的项目重新生成的pdf - 我无法将任何新更改传递给infoArray(NSMutableArray)。我猜测阵列正在释放或存在一些内存问题。有人可以帮我这个吗?

 CreateReport.m

#import "ViewReport.h"
#import "PDFRenderer.h"

-(void)preparePDF{

 //self.draftArrray has data for CreateReport tableView cells and gets updated on tableView Cells selection

NSDictionary*id_dict=[self.draftArray objectAtIndex:0];
NSArray* id_arr= [id_dict objectForKey:@"Rows"];
NSMutableArray*id_array=[[NSMutableArray alloc]init];    
id_arr= [id_dict objectForKey:@"Rows"];
NSDictionary*dict=[[NSDictionary alloc]init];
for (dict in id_arr) {
    NSString*id_string=[dict objectForKey:@"title"];
    [id_array addObject:id_string];
    }
 infoArray=id_array; //pass id_array to infoArray declared in PDFRenderer
 }
-(void)IBAction{
    [self performSegueWithIdentifier:@"finalViewReport" sender:self];
}


- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
     if ([segue.identifier isEqualToString:@"finalViewReport"]) {
      //ViewReport*destViewController = segue.destinationViewController;
       [self preparePDF];
        }
}

- * -     PDFRenderer.h

#import <Foundation/Foundation.h>

extern  NSMutableArray*infoArray;


@interface PDFRenderer : NSObject 

@property (nonatomic,strong,retain) NSMutableArray*infoArray;


+ (PDFRenderer *)sharedInstance;

- * -

 PDFRenderer.m

#import "PDFRenderer.h"

NSMutableArray*infoArray;

@implementation PDFRenderer


@synthesize infoArray;


+(PDFRenderer*) sharedInstance{
    static PDFRenderer* _shared = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    _shared = [[self alloc] init];
    _shared.infoArray = [[NSMutableArray alloc] init];

});
return _shared;
 }


+ (void)drawPageNumber:(NSInteger)pageNum
{
NSString *pageString = [NSString stringWithFormat:@"Page %d", pageNum];
UIFont *theFont = [UIFont systemFontOfSize:12];
CGSize maxSize = CGSizeMake(612, 72);

CGSize pageStringSize = [pageString sizeWithFont:theFont
                               constrainedToSize:maxSize
                                   lineBreakMode:NSLineBreakByClipping];
CGRect stringRect = CGRectMake(((612.0 - pageStringSize.width) / 2.0),
                               720.0 + ((72.0 - pageStringSize.height) / 2.0),
                               pageStringSize.width,
                               pageStringSize.height);
[pageString drawInRect:stringRect withFont:theFont];
}


+(void)drawPDF:(NSString*)fileName
{
UIGraphicsBeginPDFContextToFile(fileName, CGRectZero, nil);
NSInteger currentPage = 0;
BOOL done = NO;
  do {
   for ( int i = 0 ; i < 4 ; i++ )
   {
// Mark the beginning of a new page.

UIGraphicsBeginPDFPageWithInfo(CGRectMake(0, 0, 612, 792), nil);

   // Draw a page number at the bottom of each page.
   currentPage++;
   [self drawPageNumber:currentPage];

//[self drawText:@"Hello World" inFrame:CGRectMake(0, 0, 300, 50)];

       int  xOrigin= 50;
       int  yOrigin= 50;
       int rowHeight = 50;
       int columnWidth = 512;

       int numberOfRows = 7;
       int numberOfColumns = 1;
if (!i>0) {
[self drawLabels];

  xOrigin= 50;
  yOrigin= 300;

CGPoint from = CGPointMake(10, 60);
CGPoint to = CGPointMake(602, 60);
[PDFRenderer drawLineFromPoint:from toPoint:to];

CGPoint from1 = CGPointMake(10, 140);
CGPoint to1 = CGPointMake(602, 140);
[PDFRenderer drawLineFromPoint:from1 toPoint:to1];
}

  UIGraphicsEndPDFContext();

}


+(void)drawLabels
 {

int i;
NSString*s=[[NSString alloc]init];
int x=20;
int y=47;
for (i=0;i<infoArray.count;i++) {
    if (i==0|i==3|i==6|i==9|i==12|i==15) {
        x=20;
        y+=15;
    }
    s=[infoArray objectAtIndex:i];
   [self drawText:s inFrame:CGRectMake(x, y, 100, 25)];
    x+=185;
    y+=0;
}
}

- * -

 ViewReport.m

-(void)viewDidLoad
{

  NSString* fileName = [self getPDFFileName];
 [PDFRenderer drawPDF:fileName];


  NSURL *url = [NSURL fileURLWithPath:fileName];
  NSURLRequest *request = [NSURLRequest requestWithURL:url];
  [detailview setScalesPageToFit:YES]; //UIWebView*detailview
 [detailview loadRequest:request];
//[self.view addSubview:detailview];

}

-(NSString*)getPDFFileName
{
NSString* fileName = @"newReport.PDF";
NSArray *arrayPaths =
NSSearchPathForDirectoriesInDomains(
                                    NSDocumentDirectory,
                                    NSUserDomainMask,
                                    YES);
NSString *path = [arrayPaths objectAtIndex:0];
NSString* pdfFileName = [path stringByAppendingPathComponent:fileName];

return pdfFileName;

}

1 个答案:

答案 0 :(得分:0)

删除此行NSMutableArray*infoArray;由于您使用的是@synthesise,因此您不需要此行。

另外,我毕竟不知道你在哪里使用这个阵列?