Pass object with NSNotificationCenter to other view
Asked Answered
K

2

10

I am trying to pass an object from my main view class to other notification receiver in another class.

I want to pass an object named country, that loads all the cities from an SOAP Request in the Main Controller and i want to send it to my next view.

country = [[Country alloc] init];

Country header:

@interface Country : NSObject
{
    NSString *name;
    NSMutableArray *cities;
}

@property (nonatomic,retain) NSString *name;

- (void)addCity:(Cities *)city;
- (NSArray *)getCities;
- (int)citiesCount;    
@end

I found a way to pass data with NSNotificatios is using a NSDictionary in UserInfo. But its not possible to send the whole object instead of converting to an NSDictionary? Or what's the best way to transfer it? Im stuck trying to figure out how to pass the objects.

Actually i got working this simple NSNotification on my App.

NSNotification in the Main View Controller implementation:

//---Call the next View---
DetailViewController *detail = [self.storyboardinstantiateViewControllerWithIdentifier:@"Detail"];
[self.navigationController pushViewController:detail animated:YES]; 

//--Transfer Data to2View 
[[NSNotificationCenter defaultCenter] postNotificationName:@"citiesListComplete" object:nil];

NSNotification in 2View Controller implementation:

 // Check if MSG is RECEIVE
- (void)checkMSG:(NSNotification *)note {

    NSLog(@"Received Notification");
}

- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(checkMSG:) 
                                                 name:@"citiesListComplete" object:nil];
Krupp answered 22/11, 2011 at 20:31 Comment(0)
U
27

Oooooo, so close. I have a feeling you do not understand what an NSDictionary is though.

Post your notification with this:

Country *country = [[[Country alloc] init] autorelease];
//Populate the country object however you want

NSDictionary *dictionary = [NSDictionary dictionaryWithObject:country forKey:@"Country"];

[[NSNotificationCenter defaultCenter] postNotificationName:@"citiesListComplete" object:nil userInfo:dictionary];

then get the country object like this:

- (void)checkMSG:(NSNotification *)note {

    Country *country = [[note userInfo] valueForKey:@"Country"];

    NSLog(@"Received Notification - Country = %@", country);
}

You don't need to convert your object into a NSDictionary. Instead, you need to send a NSDictionary with your object. This allows you to send lots of information, all based on keys in the NSDictionary, with your NSNotification.

Underdone answered 22/11, 2011 at 20:48 Comment(2)
Thanks! Works like a charm. I misunderstood the concept of an NSDictionary but know its more clear.Krupp
Thanks, mate! I was using the wrong method and I was totally getting the notification but not the userInfo. Cheers.Frederiksberg
S
4

For Swift You can pass dictionary with using the below code

NSNotificationCenter.defaultCenter().postNotificationName(aName: String, object anObject: AnyObject?, userInfo aUserInfo: [NSObject : AnyObject]?)

for example

NSNotificationCenter.defaultCenter().postNotificationName("OrderCancelled", object: nil, userInfo: ["success":true])

And read this dictionary from

 func updated(notification: NSNotification){

         notification.userInfo?["success"] as! Bool 
    }
Surgeon answered 23/2, 2016 at 9:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.