Make pics Clickable in Xcode

Discussion in 'Mac Programming' started by xcoder78, Jul 9, 2007.

  1. macrumors newbie

    Joined:
    Jul 9, 2007
    #1
    How do you make pictures clickable in Xcode? Im using them in a browser im making.
     
  2. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    Do you really mean: "How do I assign my own custom icon to a NSButton"?
     
  3. macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #3
    Hey, I am curious as to how this will be done as well. I will need images and textfields to be clickable. If the only way to do this is with an NSButton, than how would I make the button part invisible so all I can see is the text (I need text with a transparent background). As well of course for the image.

    Just looking at the documentation. A NSTextField and NSImageView inherits from NSControl which has the method
    Code:
    – performClick:
    This sounds like exactly what I want.

    So... I could be completely wrong but I think you'll have to subclass, in your case an NSImageView and override the performClick method. In there you do whatever you gotta do.
     
  4. macrumors 601

    HiRez

    Joined:
    Jan 6, 2004
    Location:
    Western US
    #4
    -performClick: makes it look like the view was clicked with a mouse, but it doesn't handle an actual click event. I think what you want is to subclass and override -mouseDown:
     
  5. macrumors 68040

    MongoTheGeek

    Joined:
    Sep 13, 2003
    Location:
    Its not so much where you are as when you are.
    #5
    The trick is that you make a button and apply the image to the button. Then take the border off of the button( A check box in the attributes panel)
     
  6. thread starter macrumors newbie

    Joined:
    Jul 9, 2007
    #6
    Ok, I subclassedd NSImageView but how do i override the perform click method?
     
  7. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #7
    Did you read the above? You don't want to override performClick: you want to override mouseDown: or maybe mouseUp: or perhaps both.

    Simply add the appropriate method to the class (just a method with the same signature as that in the documentation for NSControl) and go from there.
     
  8. thread starter macrumors newbie

    Joined:
    Jul 9, 2007
  9. Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #9
    Not sure what you mean? Do you mean how do you set the image the view is displaying?

    The it's all in the documentation
     
  10. macrumors regular

    Joined:
    Feb 18, 2007
    Location:
    Canada
    #10
    Yeah i'm pretty sure that is what he means.

    [imageView setImage:theImage];

    I will be trying this mousedown mouseup stuff very shortly xcoder78. I'll show you my implementation then.

    Be sure to check out that link robbieduncan posted. Explains the methods in detail.
     
  11. macrumors newbie

    Joined:
    May 2, 2011
    #11
    Thats how you do it ;)

    You select a picture on your mac, drag it into your xcode project, add a Round Rec Button, in the Attribute Inspector you change Type: from round rec to custom. In image you select the picture you added previously. ;)
     
  12. macrumors 6502a

    Joined:
    Dec 2, 2008
    #12
    Interface Builder

    On the off chance that I understand what you're trying to do, you should set up your button in Interface Builder, set that button's picture (not background) to one of the picture assets (which you have already moved into Xcode). Then hook that button up to Xcode normally (if you have Xcode 4 this you can control drag from the button to the .h file and the IBAction and method in both the .h and .m will be set up automatically, you just put your code in the new method).

    If the picture never changes, you're done. No programming. If the picture changes, then set button.image (I think) to the new picture when necessary.
     

Share This Page