Newbie: Simple display with NSView - problem
Newbie: Simple display with NSView - problem
- Subject: Newbie: Simple display with NSView - problem
- From: Genu Mathew <email@hidden>
- Date: Wed, 1 Oct 2008 12:02:07 -0700 (PDT)
HI,
I am a new to programming in Cocoa and I am trying to write a program that has a button in the main window (MainWindow.xib) which when pushed opens another window (newWindow.xib) containing an image in it. The new window also has a custom View class called 'OUSubImageView' (subclass of NSView) that displays the image. The file name of the image is passed from the appController class to the NewWindowController class (controls the new Window) which inturn passes the file name to the OUSubimageView class which then displays the image.
The problem I am facing is that when I call the new Window and pass the file name onto the new window, the view (OUCustomView class) inside the window does not show the image. The new Window would show the image if I hard code the filename into the drawRect function inside the OUSubImageView class.
On debugging, I found that when I run the command [NewWindow showWindow:self] in the APPController class, the constructor of 'OUSubImageView is called twice implying that two new instances of the class have been created (both these instances don't contain the image to be displayed) and it is one of these instances that calls the drawRect function at the time that the window is rendered. I am not sure why this happens and how to correct it.
I would sincerely appreciate any and all help in solving this problem.
Thanks in advance
Genu
The relevant code for the various classes are
//*****************
//AppController class
// has the action for the button
#import <Cocoa/Cocoa.h>
#import "NewWindowController.h"
#import "OUSubimageView.h"
@interface AppController : NSObject {
NSWindowController * myMainWindow;
}
- (IBAction)showNewWindow:(id)sender; // aci
@end
@implementation AppController
- (id)init
{
[super init];
return self;
}
- (IBAction)showNewWindow:(id)sender
{
NewWindowController *myNewWindow;
myNewWindow=[[NewWindowController alloc]initwithImageFileName:@"/Users/deewu/Pictures/Summer2008/summer2008 005.jpg"];
[myNewWindow showWindow:self];
}
//******************
// NewWindowController class
// Class that controlls the new window
#import <Cocoa/Cocoa.h>
#import "OUSubImageView.h"
@interface NewWindowController : NSWindowController {
IBOutlet OUSubImageView * aOUSubImageView;
}
- (id)initwithImageFileName:(NSString *)fileName;
- (IBAction)showWindow:(id)sender;
@end
@implementation NewWindowController
- (id)initwithImageFileName:(NSString *)fileName{
// 1) initialize with name of the xib or nib (window) file that you want to open it
if (![super initWithWindowNibName:@"NewWindow"])
return nil;
if(!aOUSubImageView){
NSRect frame = NSMakeRect(10, 10, 100, 300);
aOUSubImageView = [[OUSubImageView alloc] initWithFrame:frame];
}
[aOUSubImageView ssetImage:fileName];
return self;
}
- (IBAction)showWindow:(id)sender
{
[super showWindow:sender];
[aOUSubImageView lockFocus];
[aOUSubImageView drawRect:[aOUSubImageView bounds]];
[aOUSubImageView unlockFocus];
}
//******************
#import <Cocoa/Cocoa.h>
@interface OUSubImageView : NSView {
NSImage *JPEGImage;
}
-(id)JPEGImage;
- (void) ssetImage:(NSString *)imageFileImage;
- (void)drawRect:(NSRect)Rect;
@end
@implementation OUSubImageView
- (id)initWithFrame:(NSRect)frame
{
if (![super initWithFrame:frame])
return nil;
return self;
}
- (void) ssetImage:(NSString *)imageFileImage{
[JPEGImage initWithContentsOfFile:imageFileImage];
[self setNeedsDisplay:true];
}
- (void)drawRect:(NSRect)Rect
{
// draw image if there is image
if(JPEGImage)
{
[JPEGImage setFlipped:NO];
NSPoint leftPoint = NSMakePoint(0, 0);
[JPEGImage compositeToPoint:leftPoint operation: NSCompositeSourceOver];
}
}
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden