0

I´m working on a CustomSlider Class. In my project I have three sliders which all have the class CustomSlider. I want these three sliders to have different knobs. See Picture.

Since the sliders already have tags I decided to use them for this purpose. Unfortunately I don´t see how to get tag value of the NSSliderCell.

I tried this:

int myTag = [self tag];

and this:

NSButton *button = [super self];
int myTag = [button tag];

Both without success, any idea? Is there another way to let the class know which object is sending the message?

Greetings, Ronald

enter image description here

Ronald Hofmann
  • 1,390
  • 2
  • 15
  • 26

1 Answers1

1

The tag is a long. NSSliderCells inherit their tag from NSActionCell, so have a tag separate from the NSSlider which uses the NSSliderCell, which inherits its tag from NSControl. In a custom NSSliderCell class, [self tag] does return the tag set for the sliderCell in the XIB.

Here is the header for a custom slider cell class:

#import <AppKit/AppKit.h>
@interface EWSliderCell : NSSliderCell
@end

and here is the whole of the class itself:

#import "EWSliderCell.h"
@implementation EWSliderCell
-(NSRect)rectOfTickMarkAtIndex:(NSInteger)index {
    static BOOL reportedTag;
    if (!reportedTag) {
        long myTag = [self tag];
        NSLog(@"myTag=%ld", myTag);
        reportedTag = TRUE;
    }
    return CGRectMake(0.0, 0.0, 0.0, 0.0); // remove the tick marks from the slider
}
@end

and this does execute and report the correct tag.

I'm not able to replicate the error message you report. However, if I (erroneously) write myTag = [EWSliderCell tag]; then I see the message "No known class method for selector 'tag'. tag is not a class method, it's an instance method. (Is that the right word?) Can you post the code which produced the error message?

emrys57
  • 6,679
  • 3
  • 39
  • 49
  • Unfortunately my class CustomSlider will be one of my Tool classes which will be used by all of my projects having sliders. Therefore I can't use IBOutlets here. The three sliders shown on the picture have iboutlets and I use tags there. That is the reason why I used super. Am I wrong? I wonder that I can set tags for NSSliderCell. – Ronald Hofmann Nov 30 '12 at 18:30
  • I have to say that I don't understand that comment at all! But I am fairly confident that the answer to your question is "YES" :-) . You can set the tag for the NSSliderCell in the xcode XIB editor, but I think the property is inherited from the NSSlider object. In what method of what class are you trying to find the tag? – emrys57 Nov 30 '12 at 18:35
  • Which part doesn´t you understand? Btw, tag is inherited from NSControl. – Ronald Hofmann Nov 30 '12 at 18:58
  • 1
    Ah! The code you are trying to write is in your custom NSSliderCell subclass! Is that right? If that's the case, `int myTag = [self tag];` should work because NSSliderCell seems to inherit its tag from (we were both wrong) NSActionCell. Did that not work? It seems to work for me, I just tried it in a custom sliderCell. What happened? – emrys57 Nov 30 '12 at 19:19
  • Yes, I didn´t write that CustomSlider inherits from NSSliderCell, sorry. That´s what I didn´t understand that 'int myTag = [self tag];' didn´t work. I´m getting a warning:'Class method '+tag' not found (return type defaults to 'id')' and myTag stays zero. – Ronald Hofmann Nov 30 '12 at 19:41
  • I found the problem. I do the initializing in a '+ (void) initializing' class method and that didn´t work. Now I moved it to another place and voila. Thanks for great hints :) – Ronald Hofmann Nov 30 '12 at 20:11
  • Hey, that's not fair! You fixed it while I was rewriting my answer! Anyway, thanks very much for the tick and congratulations on finding the bug. – emrys57 Nov 30 '12 at 20:21
  • The Hint that my expression was working at yours was very helpful. I'm pretty new to Objective-C. Is it correct to do initializing in a class method? If so what is the advantage vs applicationWillFinishLaunching? Couldn't I use that as well? – Ronald Hofmann Nov 30 '12 at 20:27
  • let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/20411/discussion-between-emrys57-and-ronald-hofmann) – emrys57 Nov 30 '12 at 20:29
  • The chat room says you've arrived, but you're very quiet. You can click on the "continue this discussion" in the previous comment to go to the chatroom. But I have to be away for 10 minutes, leave a message in chat if you want. – emrys57 Nov 30 '12 at 20:46