我已经开始了一个小项目来了解NSViewController
s。
我有一个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