2

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

  1. How do I get the main window to show the image?
  2. Is it ok to use PhotoGrabberController.view instead of creating a view subclass?
  3. 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.
  4. 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.
user1267383
  • 103
  • 1
  • 9

1 Answers1

7

How do I get the main window to show the image?

That's a view's job.

As you already have a view, you just need to get it into the window. A window only holds one view directly, which is called its content view; most probably, you will want to add the view controller's view as a subview of the view that is already the content view, or as a subview of some other grandchild view.

Is it ok to use PhotoGrabberController.view instead of creating a view subclass?

These are orthogonal. The VC's view is an instance of a view class. Creating a class has nothing to do with using an instance.

I don't think creating a view subclass will help you here. You already have two working views: a plain NSView as the VC's immediate view, and a NSImageView within it. No subclass is necessary to add these to the view—with the image view within it—to the window.

What is wrong with my understanding of how things work here?

You've missed the entire concept of the view hierarchy, and the fact that such a hierarchy is how every window displays its content.

Also, you began your question with “I'm new at objective-c” (sic), but your question is about the Cocoa framework (specifically, the AppKit framework), not the Objective-C language.

Can you direct me to a resource where I can fully understand how this works?

The Window Programming Guide (particularly, “How Windows Work”) and the View Programming Guide (particularly, “Working with the View Hierarchy”).

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Hi, Thanks for the thorough answer. I think I worded question two incorrectly. I meant to ask, what is the difference between creating PhotoGrabberController.view instead of creating an instance of a NSView subclass and using that? Is the PhotoGrabberController.view just a regular instance of NSView? – user1267383 Mar 25 '12 at 21:33
  • @user1267383: You can see what kind of object the VC's view is in the VC's nib. Select the view and look in the Outline inspector. By default, yes, a VC's view is a plain NSView. In that case, there is no difference except that one way has you create everything in the nib and the other has you create everything in code. – Peter Hosey Mar 25 '12 at 21:37
  • Not that thorough, just a nicely worded RTFM 'answer.' Code is always better. Otherwise, I'm perfectly capable of construing high level concepts myself. When someone looking for a technique accidentally clicks on an 'answer' like this, it's a complete waste of their time. – johnrubythecat Oct 23 '14 at 17:19
  • @johnrubythecat I've seen a lot of drive-by criticisms from you along this same line: no ready-to-copy-and-paste code means not only a poor answer but the answerer has somehow caused you personal insult. Lighten up. – Joshua Nozzi Oct 28 '14 at 14:33
  • @johnrubythecat In fact, of your own ten answers (the ones that aren't actually questions posted inappropriately as answers), you seem to be just as guilty as those you criticize. If you're going to call people out for "wasting your time," maybe you ought to set a better example yourself. Or ask your doctor for lithium. – Joshua Nozzi Oct 28 '14 at 14:42
  • @Joshua Nozzi, There is already too much of a stigma against people getting the health care that they need to achieve mental health, without your criticizing people who go to their psychiatrists and are prescribed lithium. – Kaydell Sep 20 '16 at 01:27
  • @Kaydell Funny how we're so PC these days we're not even allowed to suggest someone is mentally unstable ... because it might upset those who are mentally unstable.Thanks for the sermon. Now go away. – Joshua Nozzi Sep 20 '16 at 11:42