0

我有一种方法可以将输入的客户数据保存在调用 createPDFFile 方法的表单中。我已经创建了 PDF 文件,并且目前在其中有一些填充字符串,以便我可以布置 PDF 文件,我现在想从 NSTextField 中获取 stringValue,然后将其附加到包含磁贴的字符串中,例如:@ “名称:”从 NSTextField 中附加 stringValue。我可以让第一个项目工作,因为我将 IBOutput 更改为保留,并且对第一个项目有效,但对第二个项目无效,依此类推。我收到 BAD_AXCESS 错误,但仅在方法完成时。

这是 PDF 绘制方法的开始:

 -(void)drawPDFCustomerInfoWithContext:(CGContextRef)currentContext andWithPageSize:(CGSize)pageSize andCustomerObject:(Customer *)currentCustomer{
CTFontRef font = CTFontCreateWithName(CFSTR("Arial"), 12, NULL);
CFRange currentRange = CFRangeMake(0, 0);
CGContextSetTextMatrix(currentContext, CGAffineTransformIdentity);

NSString *customerName = [[[@"Name: " stringByAppendingString:self.firstNameTextField.stringValue] stringByAppendingString:@" "] stringByAppendingString:self.lastNameTextField.stringValue];
NSString *customerAddress = @"Address: ";
NSString *customerCity = @"City: ";
NSString *customerState = @"State: ";
NSString *customerZipcode = @"Zipcode: ";
NSString *customerPhone = @"Phone: ";
NSString *customerEmail = @"Email: ";

CFStringRef customerNameStringRef = (__bridge CFStringRef)customerName;
CFStringRef customerAddressStringRef = (__bridge CFStringRef)customerAddress;
CFStringRef customerCityStringRef = (__bridge CFStringRef)customerCity;
CFStringRef customerStateStringRef = (__bridge CFStringRef)customerState;
CFStringRef customerZipCodeStringRef = (__bridge CFStringRef)customerZipcode;
CFStringRef customerPhoneStringRef = (__bridge CFStringRef)customerPhone;
CFStringRef customerEmailStringRef = (__bridge CFStringRef)customerEmail;

这是保存方法:

 - (IBAction)sendEstimateToCustomer:(id)sender {
float tempFloat = [[self.estimateNumberLabel stringValue]floatValue];
self.customerDataArray = [[NSMutableArray alloc]initWithArray:[self createCustomerDataArrayWithFirstName:self.firstNameTextField.stringValue andLastName:self.lastNameTextField.stringValue andAddress:self.customerAddress.stringValue andCity:self.customerCity.stringValue andState:self.customersState.stringValue andZipCode:self.customerZipcode.stringValue andPhone:self.customerPhone.stringValue andEmail:self.customerEmail.stringValue andEstimateNumber:tempFloat]];

self.customer = [[Customer alloc]initWithCustomerDataArray:self.customerDataArray];




[self createEstimatePDF];

这是 createEstimatePDF 方法

 -(void)createEstimatePDF{
NSLog(@"create Estimate Method Fired");


CGContextRef pdfContext;
CFURLRef url;
url = [self createPDFPath];

CFMutableDictionaryRef myDictionary = NULL;
myDictionary = CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
CFDictionarySetValue(myDictionary, kCGPDFContextTitle, CFSTR("My PDF File"));
CFDictionarySetValue(myDictionary, kCGPDFContextCreator, CFSTR("Ben"));
pdfContext = CGPDFContextCreateWithURL(url,NULL, myDictionary);

CFRelease(url);

CGPDFContextBeginPage(pdfContext,myDictionary);
CGSize pageSize = CGSizeMake(612, 792);



[self drawPDFJobDescriptionTableWithContext:pdfContext andWithPageSize:pageSize];
[self drawPDFContractualTextWithContext:pdfContext andPageSize:pageSize];
[self drawPDFCustomerInfoWithContext:pdfContext andWithPageSize:pageSize andCustomerObject:self.customer];
[self drawPDFEstimateNumberWithContext:pdfContext andWithPageSize:pageSize];
[self drawPDFJobTableHeadersWithContext:pdfContext andPageSize:pageSize];
[self drawPDFBorderWithContext:pdfContext andWithPageSize:pageSize];
[self drawPDFHeaderWithContext:pdfContext andWithPageSize:pageSize];
[self drawPDFLegalStatementWith:pdfContext andPageSize:pageSize];
[self drawPDFSignatureAndDateWith:pdfContext andPageSize:pageSize];
[self drawPDFSignLinesWithContext:pdfContext andPageSize:pageSize];

CFRelease(myDictionary);


CGPDFContextEndPage(pdfContext);

我认为有些东西正在被释放,但我不知道在哪里。我观察了变量并逐步检查,它们一直显示值,然后繁荣,击中该方法的最后一个右括号,然后显示 BAD_ACCESS 错误。我不明白的是,这行得通,

 NSString *customerName = [[[@"Name: " stringByAppendingString:self.firstNameTextField.stringValue] stringByAppendingString:@" "] stringByAppendingString:self.lastNameTextField.stringValue];

我也尝试复制这些值以通过方法以及传递的客户对象传递数据。

如果需要更多信息,我可以提供。所有的 NSTextfields 都保留

4

1 回答 1

0

问题是在函数的末尾我释放了导致值在绘制之前丢失的 CFStringRef 我相信,我删除了

 CFRelease(customerNameStringRef);

然后它似乎对所有人都有效。

于 2016-08-23T08:21:59.660 回答