I'm new at objective-c so please bear with me. I have a class that returns a picture from a webcam and I am trying to display that to the screen. I subclassed NSViewController
to get the image from the camera class and set it to an instance NSImageView
and set the NSViewController
's view to be the NSImageView
. I have created a custom view in Interface Builder, dragged a NSViewController object into MainMenu.xib set it's class to be PhotoGrabberController, and control click-dragged from the custom view to the PhotoGrabberController to set it's outlet binding to be the view. (I really don't know how that works behind the scenes, it seems like magic to me). Nothing shows up on the screen and I've been playing around with this forever.
In PhotoGrabberController.h
@interface PhotoGrabberController : NSViewController {
PhotoGrabber * grabber;
NSImageView* iView;
}
@property (nonatomic, retain) NSImageView* iView;
In PhotoGrabberController.m
@implementation PhotoGrabberController
@synthesize iView,grabber;
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
}
return self;
}
- (void) awakeFromNib
{
self = [super init];
if (self) {
NSRect rect = NSMakeRect(10, 10, 200, 100);
iView = [[NSImageView alloc] initWithFrame:rect];
[iView setImageScaling:NSScaleToFit];
[iView setImage:[NSImage imageNamed:@"picture.png"]];
grabber = [[PhotoGrabber alloc] init];
grabber.delegate = (id)self;
//[grabber grabPhoto]; (This usually sets iView but I removed it and manually set iView to be an image from file above for simplicity)
[self setView:iView];
}
}
I toyed around with putting a bunch of different things in the AppDelegate's applicationDidFinishLaunching
but the way it's set up right now I think I shouldn't need to put anything in there.
Question
- How do I get the main window to show the image?
- Is it ok to use PhotoGrabberController.view instead of creating a view subclass?
- What is wrong with my understanding of how things work here? I've spent a lot of time trying to figure this out and have gotten nowhere.
- Can you direct me to a resource where I can fully understand how this works? I've found Apple documentation too detailed and thick. I just want to understand how windows, views, and view controllers are loaded and interact.