0

我已经开始了一个小项目来了解NSViewControllers。

我有一个AppController处理NSOpenPanel. 一旦我得到一个电影文件的 URL,我就将它传递给一个NSViewController子类 ( NNMovieViewController)。我就是这样做的:

-(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)"
}

头文件如下所示:

#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

我错过了什么?我在一个没有 a 的项目中重新做了整个事情,NSViewController它只是工作......

更新

在收到 Kreiri 和 Parag Bafna 的评论后,我进行了更多修改,发现在我调用[movieViewController openMovieWithURL:url];AppController 时,Outlets 还没有连接。

这是我的 AppController 实现:

#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
4

1 回答 1

0

是的,愚蠢的我。在 Interface Builder 中,我用错误的对象连接了我的控件。我应该使用 File's Owner 但我拖入了一个 NSObject 并将其类设置为NNMovieViewController并将小部件连接到它。

于 2013-07-12T09:53:16.093 回答