Dear all,

I have a question on delegate/notification techniques in Cocoa.

For example, a simple delegate/notification sample is as follows. My
question is if it is possible to transmit data from WorkingApp to
AppController? Now AppController can only get notified about the Print job
is done. If some states are created by Print, can they be transmitted to
AppController through the notification technique?

WorkingApp

    @implementation WorkingApp

    - (void) Print
    {
        NSLog(@"I am doing a tough job!");
        [[NSNotificationCenter defaultCenter] postNotificationName:@"Done"
object:self];

        // The following code cannot be compiled.
        // NSString *message = "Happy holidays!";
        // [[NSNotificationCenter defaultCenter] postNotificationName:@"Done"
object:message];
    }

    @end

Monitor

    @implementation Monitor

    @synthesize delegate;

    - (void) IHaveDone: (NSNotification *) notification
    {
        if ([self.delegate respondsToSelector:@selector(IHaveDone:)])
        {
                [self.delegate IHaveDone:self];
        }
    }

    - (void) setUpNotification: (NSString *) notification withSelector:
(SEL) methodName
    {
        [[NSNotificationCenter defaultCenter] addObserver:self
selector:methodName name:notification object:nil];
    }

    - (void) registerNotifications
    {
        [self setUpNotification:@"Done" withSelector:@selector(IHaveDone:)];
    }

    - (id) init
    {
        if (self = [super init])
        {
                [self registerNotifications];
        }
        return self;
    }
    @end

MonitorDelegate

    @class Monitor;

    @protocol MonitorDelegate

    @optional
    - (void) IHaveDone: (Monitor *) app;

    @end

AppController

    @implementation AppController

    @synthesize monitor;

    - (void) IHaveDone:(Monitor *)app
    {
        NSLog(@"I know you have done!");
    }

    - (void) sayHi
    {
        NSLog(@"Hi");
    }

    - (id) init
    {
        self.monitor = [[Monitor alloc] init];
        self.monitor.delegate = self;
        [self.monitor.delegate sayHi];
        return self;
    }

    @end
_______________________________________________

Cocoa-dev mailing list ([email protected])

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]

Reply via email to