Re: RunLoop and callback function
Re: RunLoop and callback function
- Subject: Re: RunLoop and callback function
- From: "Louis C. Sacha" <email@hidden>
- Date: Tue, 27 Jan 2004 17:14:12 -0800
Hello...
The reason that self isn't working inside the function is that the
code for functions and methods don't really belong to any particular
object. In objective-C methods, self is one of the two hidden
arguments (self and _cmd) provided when a message is sent, and it
allows the code in the method to refer to a particular object using
self and to access that objects instance variables. If you want to
learn more about this, it would be best to read the included
reference on objective-C that Apple provides (which explains things
much better than I could):
if using 10.2 developer tools
/Developer/Documentation/Cocoa/ObjectiveC/
if using 10.3 developer tools
/Developer/Documentation/Cocoa/Conceptual/ObjectiveC/
Functions don't have access to anything like self unless you pass it
as an explicit argument, and as far as I know, there isn't an easy
way to use an objective-C method as a callback function (although if
you can explicitly set up arguments to be sent to the callback
function it might be possible using methodForSelector:, but that's a
whole different can of worms, and I haven't messed around with
callback functions much so I'm not really sure if they support
functions taking arguments or not).
Probably the easiest way to handle this is to change the callback
function PowerSourcesHaveChanged() to send out a cocoa
NSNotification, and have your object that needs to respond (whatever
you wanted self to point to) register for that notification.
/* Standard warning, code written in email, so user beware... */
You would still register your callback function like you were
originally doing, but you would want to make sure you are only
registering it once for the application and not once per object (in
your primary application controller applicationWillFinishLaunching:
method, probably) and it would be changed to something like this:
void PowerSourcesHaveChanged()
{
NSLog(@"ca a changi...");
[[NSNotificationCenter defaultCenter]
postNotificationName:@"Insert your custom notification name here..."
object:nil];
}
Then any objects that needed to be notified about the power source
change would register for that notification, by including something
like the following in their initializers (and should remove
themselves as observers in their dealloc method):
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(yourSelectorNameHere:) name: @"Insert your custom
notification name here..." object:nil];
Any objects that need to do something when the power source has
changed can listen for the notification the same way, or if you
prefer you can have a single object listen for the notification and
direct the objects in what to do...
If you're not familiar with the NSNotificationCenter and
notifications, there is plenty of info in the developer docs. The
NSNotification and NSNotificationCenter classes are described in the
Foundation developer reference documentation, and there is detailed
topic about using notifications as well:
if using 10.2 developer tools
/Developer/Documentation/Cocoa/TasksAndConcepts/ProgrammingTopics/Notifications/index.html
if using 10.3 developer tools
/Developer/Documentation/Cocoa/Conceptual/Notifications/index.html
Hope that helps,
Louis
Hi,
I have a callback function which is executed when the power source
changes (this is a cocoa application).
Here is my litte source code :
******
void PowerSourcesHaveChanged()
{
NSLog(@"ca a changi...");
[self doThisPlease]; // <- self undeclared
}
/* initializePowerSourceChanges
*
* Registers a handler that gets called on power source (battery or UPS)
changes
*/
-(void)initializePowerSourceChanges
{
CFRunLoopSourceRef CFrls;
// Create and add RunLoopSource
CFrls =
IOPSNotificationCreateRunLoopSource(PowerSourcesHaveChanged, NULL);
if(CFrls) {
CFRunLoopAddSource(CFRunLoopGetCurrent(), CFrls,
kCFRunLoopDefaultMode);
CFRelease(CFrls);
}
}
******
It's works but in the function PowerSourcesHaveChanged, I get an error
because self is undeclared.
I tried to change :
IOPSNotificationCreateRunLoopSource(PowerSourcesHaveChanged, NULL);
by
IOPSNotificationCreateRunLoopSource(@selector(PowerSourcesHaveChanged),
NULL);
and void PowerSourcesHaveChanged()
by -(void)PowerSourcesHaveChanged
but my function is never called.
Have you an idea ?
Thanks you very much.
Thierry
iconless.com
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.