开发者

Cocoa Printing: Make print request percolate up to window

I'm new to Cocoa printing and trying to figure out how to take advantage of built-in NSVie开发者_StackOverflow中文版w printing. I haven't written any code or done anything in IB to enable printing, so I've just got the basic functionality built into all cocoa windows.

My problem is that if one of the NSTextFields in the window has focus, when I hit Cmd-P to print, it attempts to print just that textfield. I'd like that text field to ignore the print request so that it will percolate up to the window. I also have an NSTableView and I'd like the same to happen with that. If it has a row selected, I'd like the NSTableView to ignore the print request so, just like the textfield, it will be passed upward eventually to the window (or the NSView content view of the window).

Help?


You can change what method the Print… menu item is set up to call in your main nib file. By default, it's set to call the -print: of the first responder. In the case of a text field that has focus, it will call print: on that, which isn't what you'd like.

You could, instead, define a method such as -printWindow: in your main controller class. Then change the Print… menu item to call -printWindow: method of the first responder. Then, in that method, you could send print: to the main window's content view. The code would look something like this:

.h:

@interface MDAppController : NSObject <NSApplicationDelegate> {
    IBOutlet NSWindow *window;
}

- (IBAction)printWindow:(id)sender;

@end

.m:

@implementation MDAppController

- (IBAction)printWindow:(id)sender {
    NSLog(@"[%@ %@]", NSStringFromClass([self class]), NSStringFromSelector(_cmd));
    [[window contentView] print:sender];
}

@end

Sample project that shows this:

http://www.markdouma.com/developer/PrintWindow.zip

Regarding your comment, it sounds like in the second window/second window controller, the second window controller isn't in the responder chain, while your main window controller is in the responder chain. This could be for different reasons. Is your main window controller the application delegate? You might try making sure the second window controller is set to be the delegate of its window. That should hopefully insert the window controller into the responder chain. Otherwise, for more info on the responder chain, see Cocoa Event-Handling Guide: The Responder Chain.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜