I've started a tiny project to get my head around NSViewControllers.

I have an AppController that handles a NSOpenPanel. Once I get a URL to a movie file, I pass it to a NSViewController subclass (NNMovieViewController). This is how I do it:

-(void)openMovieWithURL:(NSURL *)url {
    NSError *error;

    movie = [[QTMovie alloc] initWithURL:url error:&error];

    [startButton setEnabled:YES];
    [movieView setMovie:movie];
    NSLog(@"button: %@", [startButton isEnabled]?@"YES":@"NO");
    // logs "NO"
    NSLog(@"movie: %@", movie);
    // logs the correct movie object
    NSLog(@"movieView: %@", [movieView movie]);
    // logs "(null)"
}

The header file looks like this:

#import <Cocoa/Cocoa.h>
#import <QTKit/QTKit.h>

@interface NNMovieViewController : NSViewController {
    QTMovie *movie;
    BOOL playing;
    IBOutlet QTMovieView *movieView;
    IBOutlet NSButton *startButton;
}

-(IBAction)clickStart:(id)sender;

-(void)openMovieWithURL:(NSURL*)url;

@end

What am I missing? I re-did the whole thing in a project without a NSViewController and it just worked...

UPDATE

After I received the comments from Kreiri and Parag Bafna I tinkered a little bit more and found out that at the time I call [movieViewController openMovieWithURL:url]; inside my AppController the Outlets are not hooked up yet.

This is my AppController implementation:

#import "AppController.h"

@implementation AppController

@synthesize movieViewController;

- (void)awakeFromNib {
    movieViewController = [[NNMovieViewController alloc] initWithNibName:@"NNMovieViewController" bundle:nil];
    NSView *viewControllerView = [movieViewController view];
    [view addSubview:viewControllerView];
}

- (IBAction)clickOpen:(id)sender {
    NSOpenPanel *dialog = [NSOpenPanel openPanel];

    [dialog setCanChooseFiles:TRUE];
    [dialog setCanChooseDirectories:FALSE];
    [dialog setAllowsMultipleSelection:FALSE];

    [dialog setAllowedFileTypes:[QTMovie movieFileTypes:0]];

    if ([dialog runModal] == NSOKButton) {

        NSURL *movieFileURL = [[dialog URLs] objectAtIndex:0];

        [self openMovie:movieFileURL];
    }
}

- (void)openMovie:(NSURL *)url {
    NSLog(@"startButton: %@", [movieViewController movieView]);
    // logs "null"
    NSLog(@"startButton: %@", [movieViewController startButton]);
    // logs "null"
    NSLog(@"---------------------------------");
    [movieViewController openMovieWithURL:url];
}

@end
有帮助吗?

解决方案

Yes, silly me. In Interface Builder I hooked up my controls with the wrong object. I should have used File's Owner but instead I dragged in an NSObject and set its class to NNMovieViewController and connected the widgets to it.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top