RE: adding a delegate to a class

2008-06-10 Thread john darnell
Without knowing what exactly you are trying to achieve, it's hard to advise 
you, Angelo.  Would a simple printf or one of its variants do for you?

R,
John

> -Original Message-
> From: [EMAIL PROTECTED]
> [mailto:[EMAIL PROTECTED] On
> Behalf Of Angelo Chen
> Sent: Tuesday, June 10, 2008 8:11 AM
> To: cocoa-dev@lists.apple.com
> Subject: adding a delegate to a class
> 
> Hi,
> 
> I have a code like this:
> 
> @interface BackgroundObj : NSObject
> {
>   Controller *mc;
> }
> 
> - (void) sayHi;
> 
> 
> @implementation BackgroundObj
> 
> - (void) sayHi
> {
>   [mc showText:@"Hi"];
> }
> @end
> 
> As you can see, the code is hard coded to use class Controller, can not be
> used with any other object, I'd like to add a delegate which will have a
> showText method, sayHi method will just call the delegate instead of hard
> coded Controller, any idea how to add delegates to this object? or, any
> tutorials for this? Thanks,
> 
> Angelo
> 
> 
> 
> 
>   Yahoo! Mail具備一流的網上安全保護功能,請前往 http://hk.antispam.yahoo.com/ 了解更多相關資訊!
> ___
> 
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
> 
> 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:
> http://lists.apple.com/mailman/options/cocoa-
> dev/john.darnell%40walsworth.com
> 
> This email sent to [EMAIL PROTECTED]
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]


Re: adding a delegate to a class

2008-06-10 Thread Jesse Armand
Hello Angelo,

I think this code might help you:

@protocol BackgroundObjDelegate 
@optional

- (void)showText:(NSString*)text;

@end

@interface BackgroundObj : NSObject
{
   id _delegate;
}

@property(assign) id delegate

- (void) sayHi;

@end

@implementation BackgroundObj

@synthesize delegate=_delegate;

- (void) sayHi
{
   if ([_delegate respondsToSelector:@selector(showText:)])
   [_delegate showText:@"Hi"];
}

@end

Then, you could implement your delegate class, for example:

@interface ForegroundObj : NSObject 
{
 // Insert your instance variables
}

- (void)showText:(NSString*)text;

@end

@implementation ForegroundObj

- (id)init
{
if (![super init])
 return nil;

BackgroundObj *obj = [[BackgroundObj alloc] init];
obj.delegate = self;// set the background object delegate

return self;
}

- (void)showText:(NSString*)text
{
// Insert your code
}

@end

If you have any questions, just ask, and please read Apple Docs, since
it's already quite complete in explaining these.

Regards,

Jesse
-
barablu (www.barablu.com), iPhone developer (http://jessearm.blogspot.com)

2008/6/10 Angelo Chen <[EMAIL PROTECTED]>:
> Hi,
>
> I have a code like this:
>
> @interface BackgroundObj : NSObject
> {
>Controller *mc;
> }
>
> - (void) sayHi;
>
>
> @implementation BackgroundObj
>
> - (void) sayHi
> {
>[mc showText:@"Hi"];
> }
> @end
>
> As you can see, the code is hard coded to use class Controller, can not be 
> used with any other object, I'd like to add a delegate which will have a 
> showText method, sayHi method will just call the delegate instead of hard 
> coded Controller, any idea how to add delegates to this object? or, any 
> tutorials for this? Thanks,
>
> Angelo
>
>
>
>
>  Yahoo! Mail具備一流的網上安全保護功能,請前往 http://hk.antispam.yahoo.com/ 了解更多相關資訊!
> ___
>
> Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
>
> 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:
> http://lists.apple.com/mailman/options/cocoa-dev/mnemonic.fx%40gmail.com
>
> This email sent to [EMAIL PROTECTED]
>
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Re: adding a delegate to a class

2008-06-10 Thread Erik Buck
See 
http://developer.apple.com/documentation/Cocoa/Conceptual/CocoaFundamentals/CommunicatingWithObjects/chapter_6_section_4.html
___

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]