Trying to append a string to another string from a NSTextField for printing on a PDF file

64 Views Asked by At

I have a method that saves the customer data entered in on the form that calls a method to createPDFFile. I have the PDF file created and I have some filler strings in there currently so that I could lay out the PDF file and I now want to take the stringValue from the NSTextField and then append it to the String that contains the tile for ex: @"Name :" the append the stringValue from the NSTextField. I can get the first item to work as I changed the IBOutput to retain and that worked for the first item but not the second and so forth. I am getting the BAD_AXCESS error but only when the method completes.

Here is the start of the PDF draw method:

 -(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;

Here is the save method:

 - (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];

Here is the createEstimatePDF method

 -(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);

I think some thing is getting released but I cannot find out where. I watched the variables and stepped through and they show the value all the way through and then boom, hits the last closing bracket for the method and then shows the BAD_ACCESS error. What I don't under stand is that this works,

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

Ive tried to copy the values over as well to pass the data through the methods as well with the customer object being passed.

If more info is needed I can provided. All of the NSTextfields are retain

1

There are 1 best solutions below

0
On

The issue was at the end of the function I was releasing the CFStringRef that was causing the value to be lost before it was drawn I believe, I removed the

 CFRelease(customerNameStringRef);

and then it seems to work for all of them.