Search code examples
objective-cmacosinterface-buildersubclassnsbutton

Subclass Programmatically Added NSButton?


I have a bunch of programmatically created NSButtons in my app. How can I go about setting the subclass of these NSButtons without using Interface Builder?


Solution

  • When you create the button, don't create them as NSButtons, create them as your subclass. For example, if you had a subclass of NSButton called MyButton, instead of something like:

    NSButton *button = [[NSButton alloc] initWithFrame:frame];
    

    You would have something like:

    MyButton *button = [[MyButton alloc] initWithFrame:frame];
    

    This way button will be an instance of MyButton.