我已经开始了一个小项目来了解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
我缺少什么?我在一个没有NSViewController
的项目中完成了整个事情而且它刚刚起作用......
更新
在我收到Kreiri和Parag Bafna的评论之后,我又多了一点,发现当我在我的AppController中调用[movieViewController openMovieWithURL:url];
时,奥特莱斯还没有连接起来。
这是我的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
答案 0 :(得分:0)
是的,傻我。在Interface Builder中,我用错误的对象连接了我的控件。我应该使用了文件的所有者,但是我拖入了NSObject并将其类设置为NNMovieViewController
并将小部件连接到它。