Why Does Nothing Print

Discussion in 'Mac Programming' started by kaydell.leavitt, Apr 27, 2010.

  1. kaydell.leavitt macrumors regular

    Joined:
    Apr 19, 2010
    #1
    I have the following code as my controller to try to print a view. The print job dialog displays, but nothing prints:

    Code:
    //
    //  PrintTestAppDelegate.m
    //  PrintTest
    //
    //  Created by kaydell on 4/27/10.
    //  Copyright 2010 LevSolve.com. All rights reserved.
    //
    
    #import "PrintTestAppDelegate.h"
    
    @implementation PrintTestAppDelegate
    
    @synthesize window;
    @synthesize printTestView;
    
    - (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
    	// Insert code here to initialize your application 
    }
    
    - (IBAction) print: (id) sender {
    	NSLog(@"print:");
    	NSPrintInfo *printInfo = [NSPrintInfo sharedPrintInfo];
    	NSPrintOperation *printOp = [NSPrintOperation printOperationWithView:printTestView printInfo:printInfo];
    	[printOp runOperation]; // <<<<<<<<<<< this displays a print job dialog <<<<<<<<<<<<<<<
    }
    
    @end
    
    
    //
    //  PrintTestView.m
    //  PrintTest
    //
    //  Created by kaydell on 4/27/10.
    //  Copyright 2010 LevSolve.com. All rights reserved.
    //
    
    #import "PrintTestView.h"
    
    
    @implementation PrintTestView
    
    - (void)drawString:(NSString *)string centeredInRect:(NSRect)r withAttributes:(NSDictionary *)theAttribute
    {
    	NSPoint stringOrigin;
    	NSSize stringSize;
    	stringSize = [string sizeWithAttributes:theAttribute];
    	stringOrigin.x = r.origin.x + (r.size.width - stringSize.width)/2;
    	stringOrigin.y = r.origin.y  + (r.size.height - stringSize.height)/2;
    	[string drawAtPoint:stringOrigin withAttributes:theAttribute];
    }
    
    - (void) drawHeaderRect: (NSRect) dirtyRect {
    	NSRect bounds = [self bounds];
    	[self drawString:@"Header" centeredInRect:bounds withAttributes:nil];	
    }
    
    - (void) drawBodyRect: (NSRect) rect {
    	NSRect bounds = [self bounds];
    	[self drawString:@"Body" centeredInRect:bounds withAttributes:nil];	
    }
    
    - (void) drawFooterRect: (NSRect) rect {
    	NSRect bounds = [self bounds];
    	[self drawString:@"Footer" centeredInRect:bounds withAttributes:nil];	
    }
    
    - (void) drawRect: (NSRect) rect { <<<<<<<<< this is never called <<<<<<<<<<<<<<<<<<<
    	if (![NSGraphicsContext currentContextDrawingToScreen]) {
    		[self drawHeaderRect:rect];
    		[self drawBodyRect:rect];
    		[self drawFooterRect:rect];
    	}
    }
    
    @end
    
    -- Kaydell
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Where do you create printTestView? If you only have the code posted then this variable is nil. Declaring a property is just declaring a variable. Synthensizing a property creates methods to set/get the variable. Neither of these create an object to store in the variable.
     
  3. kaydell.leavitt thread starter macrumors regular

    Joined:
    Apr 19, 2010
    #3
    Thanks

    Thanks, now a page prints, but it still doesn't hit my drawRect: method.
     

Share This Page