objective c - Making a custom NSButton that has an on/off state, but still handles IBActions -


so creating subclassed nsbutton has custom on/off state.

to so, overrided -mousedown , -mouseup, , modified view accordingly.

now in view controller owns button, have ibaction connected custom button.

the problem when overrided nsresponder methods, ibaction never gets called unless call [super mousedown] within overrided -mousedown method.

but if call [super mousedown], custom button doesn't receive -mouseup event (and ui doesn't update off/unpressed state).

i've tried few things, seems common thing people have customized, , should easier i'm making it.

i thinking of creating block property on custom button called pressedaction, , when view controller instantiated, set block property on button code have had in ibaction. inside of custom button, execute block in -mouseup method

but solution seems little weird because me subclassed nsbutton should able fire ibactions

i have done couple of ways in past. instead of overriding -mousedown , -mouseup used views layer , nscell property

ishighlighted 

your nsbutton subclass this

- (bool)wantsupdatelayer {     return yes; }  - (void)updatelayer {     if ([self.cell ishighlighted]) {         self.layer.contents = [nsimage imagenamed:@"onstateimage"];     } else {         self.layer.contents = [nsimage imagenamed:@"offstateimage"];     } } 

or instead of subclassing nsbutton can subclass nsbuttoncell , override - (void)drawbezelwithframe:(nsrect)frame inview:(nsview *)controlview

and again use ishighlighted property

- (void)drawbezelwithframe:(nsrect)frame inview:(nsview *)controlview {      if (self.ishighlighted) {         //draw on state     } else {         //draw off state     } } 

Comments

Popular posts from this blog

python - pip install -U PySide error -

arrays - C++ error: a brace-enclosed initializer is not allowed here before ‘{’ token -

cytoscape.js - How to add nodes to Dagre layout with Cytoscape -