Skip an object with NSNotificationCenter to another view

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

I want to pass an object named country that loads all cities from a SOAP request to the Main Controller, and I want to send it to the 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 transfer data using NSNotificatios using NSDictionary in UserInfo. But is it impossible to send the whole object instead of converting to NSDictionary? Or what is the best way to convey it? I was stuck trying to figure out how to convey objects.

Actually, I got this simple NSNotification in my application.

NSNotification in the implementation of the Main View controller:

 //---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 the implementation of 2View Controller:

  // 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]; 
+8
iphone xcode nsnotificationcenter nsnotifications
source share
2 answers

Oooooo, so close. I have a feeling that you do not understand what NSDictionary .

Send your notice as follows:

 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 as follows:

 - (void)checkMSG:(NSNotification *)note { Country *country = [[note userInfo] valueForKey:@"Country"]; NSLog(@"Received Notification - Country = %@", country); } 

You do not need to convert your object to NSDictionary . Instead, you need to send an NSDictionary with your object. This allows you to send a lot of information, all based on keys in NSDictionary , using NSNotification .

+25
source share

For Swift, you can pass the dictionary using the code below

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

eg

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

And read this dictionary from

  func updated(notification: NSNotification){ notification.userInfo?["success"] as! Bool } 
+4
source share

All Articles