NSViewController中的异常行为



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

我有一个处理NSOpenPanelAppController。一旦我得到一个电影文件的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

我缺少什么我在一个没有NSViewController的项目中重新做了整件事,它只是起了作用。。。

更新

在我收到Kreiri和Parag Bafna的评论后,我又做了一点修改,发现当我在AppController中调用[movieViewController openMovieWithURL:url];时,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
是的,我真傻。在接口生成器中,我用错误的对象连接了控件。我应该使用File的Owner,但我拖动了一个NSObject,并将其类设置为NNMovieViewController,并将小部件连接到它。

相关内容

  • 没有找到相关文章

最新更新