Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
Does anybody know how can I get a mouse hover event. I want to handle this event because I want to change the image on the button when mouse is over it.

Any suggestions are welcome.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Setup a tracking rect/area for the view. On 10.5+ use addTrackingArea: and on 10.4+ use addTrackingRect:eek:wner:userData:assumeInside:. Then the view will receive mouseEntered: and mouseExited: events which you can use to set the image.
 

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
I am somehow not able to set tracking area for the view. Any ideas in which function I will have to do this and how?

My button is on NSTabView.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Make sure you're doing this inside a subclass of NSButton because you need to override the two mouse event methods, and you should setup the tracking rects inside the viewDidMoveToWindow method.

If you're still having trouble post your code.
 

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
Thanks a bunch.. I have tried the following on a simple project :

1. Made a new XCode Cocoa Project
2. In the default window added a button
3. Added Objective C controller class which contains referneces for the Button and the ContentView of the mainwindow.
4.Subclass NSButton.
5. Added the method viewDidMoveToWindow() and mouse entered and mouseexited methods to the NSButton subclass.

This is not working... game me some exception. Have I done something wrong here? Do I need to subclass view as well?


Later I subclassed mainWindow's ContentView and added mouseentered and mouseexited functions in it as well... but it did not work either :confused:

My code has become untidy after trying many things ... so I'll clean it and post it later.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
Here's a little project that demonstrates this. Notice that in the nib the button's class is set to the NSButton subclass name.
 

Attachments

  • HoverButton.zip
    132.4 KB · Views: 1,328

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
My goodness. It was as simple as this. Thanks to you for this. :)

Its working now.
 

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
I am now trying to implement this for all the buttons that I have in my app. For this I have taken following in the HoverButton.h class

Code:
        NSString *normalImageName;
	NSString *hoverImageName;
	
	- (void)setNormalImageName:(NSString *)aString;
	- (void)setHoverImageName:(NSString *)aString;

and the following in HoverButton.m class
Code:
- (void)setNormalImageName:(NSString *)aString
{
	normalImageName = aString;
}
- (void)setHoverImageName:(NSString *)aString
{
	hoverImageName = aString;
}

Also I have included Hoverbutton.h in the delegate class and m trying to set the image names in delegate.m
as
Code:
-(void)awakeFromNib
{
	[button setNormalImageName:@"test.jpg"];

}

Here I have declared button as type of HoverButton.

I am gettting compile error "syntax error before '-' token in Hoverbutton.h.

Am I missing something here?
 

robbieduncan

Moderator emeritus
Jul 24, 2002
25,611
893
Harrogate
Well, I'm pretty sure HoverButton.cpp should be HoverButton.m otherwise the compiler will treat it as C++ code, not Objective-C.
 

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
Yes thanks for the correction.. the file is actually Hoverbutton.m and i made a mistake while typing.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
If you're still getting the syntax error post your entire class, both the .h and .m files. If you copy and paste chunks of code that you think are related it usually is the wrong part :)

Also, unless you have GC enabled, those accessors are incorrect and should look something like this (it'd be better to replace them with @properties if you can):

Code:
- (void)setHoverImageName:(NSString *)aString
{
    if (hoverImageName != aString) {
        [hoverImageName release];
        hoverImageName = [aString copy];
    }
}
 

McBgnr

macrumors regular
Original poster
Apr 13, 2009
144
0
Figured out the reason of compile issue... and the code is working perfectly fine now.

Thanks for pointing out the flaw in the accessor body... these tips are so helpful :)
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.