-1

I need to send a message to multiple objects at a time, and then pass a value back from each. Delegates can be used only one at a time, and notifications cannot return values.

Is there any way that a tab bar controller containing two view controllers can notify both of them at the same time, with return values?

jscs
  • 63,694
  • 13
  • 151
  • 195
S.J
  • 3,063
  • 3
  • 33
  • 66
  • you can pass value in using Notifications... – Sunny Shah Jun 30 '14 at 18:10
  • @SunnyShah and how you will return values using notification? – S.J Jun 30 '14 at 18:12
  • @S.J why you can't simply wrap it in some method which will return a value you need but also sends a notification ? If I understand the problem... – fgeorgiew Jun 30 '14 at 18:18
  • possible duplicate of [pass NSString variable to other class with NSNotification](http://stackoverflow.com/questions/10272068/pass-nsstring-variable-to-other-class-with-nsnotification) – Sunny Shah Jun 30 '14 at 18:19

4 Answers4

1

I assume the question is how to have the recipient of the notification to send a response back to the sender of the notification.

You can't just return a value from a notification. But the notification handler can call a method back in the original object in order to supply whatever data you wanted to pass back to it.

One way to do that is to supply an object parameter of postNotificationName that specifies who sent the notification. And then have that object (the notificationSender) conform to some protocol with some established API. For example, define a protocol for the method that the view controllers will call:

@protocol MyNotificationProtocol <NSObject>

- (void)didReceiveNotificationResponse:(NSString *)response;

@end

Then, the object issuing the notification would conform to this protocol:

@interface MyObject : NSObject <MyNotificationProtocol>
@end

@implementation MyObject

- (void)notify
{
    NSDictionary *userInfo = ...;

    // note, supply a `object` that is the `notificationSender`

    [[NSNotificationCenter defaultCenter] postNotificationName:kNotificationName object:self userInfo:userInfo];
}

- (void)didReceiveNotificationResponse:(NSString *)response
{
    NSLog(@"response = %@", response);
}

@end

Then, the view controllers that receive the notification could use the object parameter of the NSNotification object to send the response:

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didReceiveNotification:) name:kNotificationName object:nil];
}

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)didReceiveNotification:(NSNotification *)notification
{
    id<MyNotificationProtocol> sender = notification.object;

    // create your response to the notification however you want

    NSString *response = ...

    // now send the response

    if ([sender conformsToProtocol:@protocol(MyNotificationProtocol)]) {
        [sender didReceiveNotificationResponse:response];
    }
}

In the above example, the response is a NSString, but you could use whatever type of parameter to didReceiveNotificationResponse that you want, or add additional parameters (e.g. the sender of the response).

Rob
  • 415,655
  • 72
  • 787
  • 1,044
0

You can create a key-value pair and pass that in a notification's userInfo dictionary:

[[NSNotificationCenter defaultCenter] postNotificationName:myNotification object:nil userInfo:myDictionary];

In the method where you handle the notification:

- (void)handleMyNotification:(NSNotification *)notification
{
     NSDictionary *myDictionary = notification.userInfo;
}
Steve
  • 1,840
  • 17
  • 20
0

You can pass user info dictionary to the notification:

NSDictionary *dataDict = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:isReachable] 
                                                 forKey:@"isReachable"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"reachabilityChanged" object:self userInfo:dataDict];

It seems that the problem is different then I thought before. If you just want to send notification and return some value right after inside the object which sends the notification I would probably wrap it with some method

- (NSString *)notifyAboutSomethingFancy
{
  //the code from above  
  return @"whatever you want to return";
}
fgeorgiew
  • 1,194
  • 2
  • 8
  • 25
0

You can use NSNotification to pass value to different controllers

Check the relevant link.

pass data in the userDictionary

Community
  • 1
  • 1
Sunny Shah
  • 12,990
  • 9
  • 50
  • 86