Re: Callbacks
Re: Callbacks
- Subject: Re: Callbacks
- From: Alastair Houghton <email@hidden>
- Date: Wed, 18 Jul 2007 11:08:40 +0100
On 18 Jul 2007, at 10:53, Barry wrote:
Trying to add an NSProgressIndicator to my App.
In MyDocument.mm I have
-(void)incProgress
{
[progress incrementBy:1];
}
-(IBAction)doBigStuff:(id)sender
{
......
doStuff(params, ... ,incProgress);
....
}
but get error:'incProgress' was not declared in this scope.
incProgress is a method, not a function. You cannot take the address
of a method by giving its name, because it's possible that someone
else has redefined the method at runtime for some reason. This is
actually not uncommon, particularly where KVO is in use.
Anyway, in order to invoke your incProgress method, you will need (at
the very least) to pass "self" into your doStuff() function. Since I
can understand that you may not wish to have Cocoa code inside doStuff
() (e.g. because doStuff() is supposed to be portable to some other
platform), a good way to do this is to provide a function pointer
argument *and* a void pointer argument that gets passed as its
parameter. So you end up with something like this:
// The function you're going to use
static void myIncrementProgressFunction (void *foo) {
MyObj *obj = (MyObj *)foo;
[foo incProgress];
}
// Revised prototype for doStuff()
void doStuff(param_t *params, ..., void (*fn)(void *), void
*userdata);
-(IBAction)doBigStuff:(id)sender
{
...
doStuff(params, ... , myIncrementProgressFunction, self);
...
}
Also, I wouldn't advise using an incremental approach to progress
computation; I think it's usually best to calculate the current
progress value, rather than trying to continually update it. This
avoids rounding errors and means that you're less likely to make
other mistakes (e.g. with an incremental approach, if you forget to
reset the progress bar, it will display the wrong value; setting the
absolute value each time means that can't happen).
It also means that you can easily display the progress in some other
format (e.g. as a text field) to the user, without relying on the
NSProgressIndicator for data storage (IMO you shouldn't use controls
for data storage... it breaks MVC and that's usually a bad sign).
Kind regards,
Alastair.
--
http://alastairs-place.net
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden
References: | |
| >Callbacks (From: "Barry" <email@hidden>) |