PDA

View Full Version : mouse event




stece
Oct 30, 2008, 07:22 PM
Dear all
I have a code like this:


#import <Cocoa/Cocoa.h>


@interface Clasa : NSObject {
IBOutlet NSTextField *textField;
}
-(IBAction)seed:(id)sender;
-(IBAction)generate:(id)sender;
@end
---------------------------------------------
#import "Clasa.h"


@implementation Clasa
- (IBAction)generate:(id)sender
{
int generated;
generated = (random()% 100) + 1;
NSLog(@"generated = %d", generated);
[textField setIntValue:generated];
}
- (IBAction)seed:(id)sender
{
srandom(time(NULL));
[textField setStringValue:@"Generator seeded"];
}

@end

and how does the computer know that if I push the button it is to
show the int generated. I mean how does he know it in the function of time ( namely how does he know when to do it) because there is no mouseclick event defined or anything similar. So which event punctuates it and how?
I read there are some actions, but how do these actions refer to the mouse click or push of the button?

Thank you in advance



SydneyDev
Oct 30, 2008, 08:15 PM
The button object stores in it's member variables a pointer to another object, and the name of a method to call on that object when it is clicked.

In Interface Builder, you create an instance of your object (above) and drag a line from the button to your object to set these member variables. The settings you make here are saved in the Interface Builder XIB file and restored at runtime.

stece
Oct 31, 2008, 04:54 AM
The button object stores in it's member variables a pointer to another object, and the name of a method to call on that object when it is clicked.


How to know from reference library of the NSButton which member variables are pointers to another object, and how to know which method they call if I click the button ?

SydneyDev
Oct 31, 2008, 05:06 AM
It's in the NSControl parent class: setTarget (the object) and setAction (the method).

It is called the "Target-Action mechanism":
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_5.html