Frage

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.
War es hilfreich?

Lösung

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”).

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top