0

I know it seems to be a silly question however its vexed me a lot that's why I am putting it on Stackoverflow. Please be a little lenient.

Here is my NSButton:

enter image description here

there are 2 NSLabels on it

  1. showing Name of the button Installed Apps

  2. showing count of installed apps

Problem

When I click near NSLabel, the NSButton action does not get called.

P.S: I am from WPF world of Microsoft. There we had MouseDownPreview events to handle these type of situations. I don't know much about COCOA. Please help, many thanks...

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
Vikas Bansal
  • 10,662
  • 14
  • 58
  • 100

2 Answers2

2

The main issue is with NSTextField. The textfield is drawn on top of the NSButton hence it covers it. And click on the area of textfield doesn't send the mouseClick event to NSButton.

You can have few ways to cope up with this:

  1. Instead of NSTextField create NSButtons and set the action method of round button.
  2. Use a custom NSTextField that can accept mouse click event, and call the round button's action.
@interface ClickableTextField : NSTextField
@end

@implementation ClickableTextField

- (void)mouseDown:(NSEvent *)theEvent
{
    [self sendAction:[self action] to:[self target]];
}

@end
Community
  • 1
  • 1
Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
1

Subclass NSTextField and override following method to return nil.

override func hitTest(aPoint: NSPoint) -> NSView? {
    return nil
}
jessex
  • 201
  • 1
  • 4