NSViewController mouseDown:未调用

时间:2014-01-02 07:09:53

标签: cocoa nswindowcontroller nsviewcontroller nsresponder

我有一个NSWindowController,我正在添加相应的ViewControllers。我想在其中一个ViewController中处理触摸事件。我的班级是

window.h中

@interface WindowController : NSWindowController<NSToolbarDelegate>

@property   (nonatomic, strong)    NSViewController                  *currentViewController;

@property   (assign)               IBOutlet        NSView            *targetView;

@end

Window.m

-(void)addViewController
{
NSViewController *currentController = [[currentControllerClass alloc]initWithNibName:controllerIdentifier bundle:nil];
self.currentViewController = currentController;
[self.targetView addSubview:self.currentViewController.view];    
[self.currentViewController.view setFrame: [self.targetView bounds]];
}
-(void) awakeFromNib{

    [super awakeFromNib];
    [self.window setIgnoresMouseEvents:NO];
    [self setToolbarToPracticeView];
}

-(void)mouseDown:(NSEvent *)theEvent
{
    NSLog(@"Window > Mouse down");
}

我可以在这里打印mousedown事件。现在我有一个viewController,它是

ViewController.h

@interface ViewController : NSViewController
@end

ViewController.m

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Initialization code here.
    }
    return self;
}

-(void)awakeFromNib
{
    [self.view setAcceptsTouchEvents:YES];
}

-(BOOL)acceptsFirstResponder
{
    return YES;
}
-(void)mouseDown:(NSEvent *)theEvent
{
    NSLog(@"ViewController > mouse down");
}

我想在ViewController中获得mouseDown的控件。我在这里错过了什么吗?

感谢。

2 个答案:

答案 0 :(得分:4)

创建自定义NSView,然后在其协议中委托mouseDown方法。在ViewController中导入并使用customView并委派其方法。单击视图时会触发mouseDown事件。

答案 1 :(得分:0)

不确定我的答案,但是如果你没有在Window.m中调用[super mouseDown:theEvent](顺便说一下,它应该被称为WindowController.m),那么你就会打破响应者链。因此您的视图控制器无法看到它。我是对的吗?