1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

Subclassing NSButton

Discussion in 'Mac Programming' started by Blakeasd, Sep 27, 2010.

  1. macrumors 6502a

    #1
    Hello!
    Does anyone know of a good tutorial on making custom buttons by subclassing them? I have looked all over and have not found one that has been helpful. Please do not link me to any Apple Documentation.
    Thanks,
    -Blake
     
  2. macrumors 6502a

    #2
    It would be helpful to know what you are trying to do with it, and why you cannot use delegates to get what you need. In Cocoa/Obj-C you don't really subclass things all that often, esp. when compared to languages like Java.
     
  3. macrumors 603

    #3
  4. macrumors 6502a

    #4
    I want to try to make iPhone type buttons(Just how they look). I know that doesn't follow human-interface guidelines, but I need it for something I am doing. Anyway please respond and thanks :)
    -Blake
     
  5. macrumors 6502a

    #5
    https://sites.google.com/site/randomprogrammingsamples/iPhoneBtn.tgz?attredirects=0&d=1

    This should get you started, you will obviously want a better image(probl with transparency as well), I just took a quick screenshot of a button from the iPhone simulator.

    The basics is that you have to subclass NSButtonCell and override a few of NSCells methods(namely the initialization and drawing methods). You also have to make sure to keep everything sized appropriately, this code does that.

    You also have to make sure you change the class of the button's CELL in IB, you can also just copy/paste the button once you have one setup.

    Hope this helps.
     
  6. Moderator emeritus

    kainjow

    #6
    ^ missing image ;)

    iphonebtn_missing.png
     
  7. macrumors 6502a

    #7
    Oops, forgot to actually have xcode copy the image when I imported it!

    Fixed, original link should now point to the new version!
     
  8. macrumors 6502a

    #8
    This is a stupid question, but how exactly to I drag a NSButtonCell to my window. It just drags back to the library?
     
  9. macrumors 68000

    Sydde

    #9
    Switch your nib window (the window that shows the nib contents) to list view. From there you can expand items until you see your button. Select the cell (which you get to by expanding the button) and set the cell's class in the info pane of the inspector panel.
     
  10. Moderator emeritus

    kainjow

    #10
    You can also create a custom button appearance without subclassing. Just create the on and off images, then create your button in Interface Builder as a Square Button. Add the images to your project, and set them as the Image and Alt. Image on the button. Set the Border to off, and change the Type to Momentary Change.

    Reasons to subclass would be: nice appearance no matter the size of the button (can also be done with vector graphics too, e.g. PDF), mouse-over effects, and custom text styling.

    Which of the iPhone buttons are you trying to reproduce? Can you provide a screenshot?
     

Share This Page