Pregunta

I have a NSWindowController where I'm adding my corresponding ViewControllers. I would like to handle touch events in one of the ViewController. My class is

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

I'm able to print the mousedown event here. Now I have a viewController, which is

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

I would like to get the control of the mouseDown in the ViewController. Am I missing something here?

Thanks.

¿Fue útil?

Solución

Create a Custom NSView and then delegate a mouseDown method in it's protocol. Import and use the customView in the ViewController and delegate its methods. When you click on the view a mouseDown event triggers.

Otros consejos

Not sure of my answer, but if you don't call [super mouseDown:theEvent] in the Window.m (which should be called WindowController.m by the way), you are breaking the responder chain. Hence your view controller can't see it. Am I right?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top