Is it possible to assign a class to an object in code? I am creating a button dynamically, loading an image for the button, and I have something like the following:
myButton = [[[NSButton alloc] initWithFrame:NSMakeRect(100,100,50,25)] autorelease];
[myButton setTitle:@"Done"];
[myButton setTarget:self];
[myButton setAction:@selector(mySelector:)];
[myButton setButtonType:NSMomentaryChangeButton];
[myButton setBordered:NO];
[myButton setImage:[NSImage imageNamed:@"myButton.png"]];
[myButton setImagePosition: NSImageOnly];
[[[self window] contentView] addSubview:myButton];
I also have an NSButton subclass (e.g. MyButtonClass.h/MyButtonClass.m) that I want to use for the button so that it has a particular behavior, but I’m not sure how to assign that class to the button in the above code.
Is this possible, or can/should it be done only in Interface Builder?
You can initiate your button with
MyButtonClassinstead ofNSButtonmyButton = [[[MyButtonClass alloc] initWithFrame:NSMakeRect(100,100,50,25)] autorelease];