PDA

View Full Version : Make pics Clickable in Xcode




xcoder78
Jul 9, 2007, 03:37 PM
How do you make pictures clickable in Xcode? Im using them in a browser im making.



robbieduncan
Jul 9, 2007, 03:52 PM
Do you really mean: "How do I assign my own custom icon to a NSButton"?

Spike099
Jul 9, 2007, 07:19 PM
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 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.

HiRez
Jul 9, 2007, 11:13 PM
-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:

MongoTheGeek
Jul 10, 2007, 10:07 AM
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)

xcoder78
Jul 10, 2007, 10:07 AM
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 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.

Ok, I subclassedd NSImageView but how do i override the perform click method?

robbieduncan
Jul 10, 2007, 10:12 AM
Ok, I subclassedd NSImageView but how do i override the perform click method?

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.

xcoder78
Jul 10, 2007, 10:13 AM
How do you apply the image again?:confused:

robbieduncan
Jul 10, 2007, 10:31 AM
How do you apply the image again?:confused:

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 (http://developer.apple.com/documentation/Cocoa/Reference/ApplicationKit/Classes/NSImageView_Class/Reference/Reference.html#//apple_ref/occ/instm/NSImageView/setImage:)

Spike099
Jul 10, 2007, 12:41 PM
Not sure what you mean? Do you mean how do you set the image the view is displaying?

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.

IphonePrgerG
May 2, 2011, 11:19 AM
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. ;)

PatrickCocoa
May 2, 2011, 12:59 PM
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.