• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Notify With Parameters
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Notify With Parameters


  • Subject: Notify With Parameters
  • From: Bing Li <email@hidden>
  • Date: Thu, 02 Jun 2011 14:45:46 +0800

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@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

  • Follow-Ups:
    • Re: Code style (was: Notify With Parameters)
      • From: Conrad Shultz <email@hidden>
    • Re: Notify With Parameters
      • From: Jens Alfke <email@hidden>
  • Prev by Date: Re: Scaling a NSImage not working
  • Next by Date: Re: Scaling a NSImage not working
  • Previous by thread: Re: CoreData problem - AttributeDescription seems invalid --- Where am I wrong?
  • Next by thread: Re: Notify With Parameters
  • Index(es):
    • Date
    • Thread