setHidden with NSSlider doesn't work - Objective C

80 Views Asked by At

Hy guys, I'm new at ObjC and I'm still learning; [sliderContrast setHidden:YES] (i also used slider.hidden = YES) doesn't make the slider invisible, instead it works fine with textfields. Do you know why? I've also tried using property and synthesize but the result doesn't change

---Interface

@interface Controller : NSWindowController{
 
    IBOutlet NSTextField *labelContrast;
    IBOutlet NSTextField *valueContrast;
    IBOutlet NSSlider *sliderContrast;
}
- (IBAction)changeContrast:(id)sender;

@end

---Implementation

#import "Controller.h"

@interface Controller ()

@end


@implementation Controller

- (void)windowDidLoad {

    [super windowDidLoad];

    [labelContrast setHidden:YES];

    [valueContrast setHidden:YES];

    [sliderContrast setHidden:YES];

}

- (IBAction)changeContrast:(id)sender {
}
@end
1

There are 1 best solutions below

0
On

If you declare pointers for your objects but you don't allocate them yourself you can not set anything that is not there. Your setHidden: method calls end up in local void aka nowhere.

programmatically

If you go the coding way you would declare, allocate and initiate first. With

labelContrast = [NSTextField alloc] initWithFrame:NSMakeRect(x,y,w,h)];

before you call other methods of the object class (Or similar init methods).
After that you can call methods on the object.

Almost all objects inherit an according
-(instancetype)init or -(instancetype)initWith... method you can use. If there is no init method given, then there is another way to do it right and the moment to start reading again :).

With Interface Builder

By typing IBOutlet or IBAction in front of a declaration you just give a hint for Xcodes Interface Builder where to hook up and apply onto (associate) the placed object in (nib,xib,storyboard) with its object ID in the XML scheme to a reference in code.

So after you connected your code and the object in IB you can avoid allocation and init process for that particular object. Be aware that calling methods on objects that are not instanced yet is not working. Which is why you code in - (void)windowDidLoad, -(void)viewDidLoad or -(void)awakeFromNib because those are the methods that get called after "IB" has done its job for you.