0

I am using NSNotification to pass value between ViewControllers, but value is not getting passed and when i used breakpoints to check what's going wrong i came to know that the receive notification selector method is not called. Following is the code what i have written

AViewController.m

[[NSNotificationCenter defaultCenter] postNotificationName:@"speciality" object:nil userInfo:[specialityIdArray objectAtIndex:indexPath.row]];

BViewCOntroller.m

-(void)viewWillAppear:(BOOL)animated
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSpecialityId:) name:@"speciality" object:nil];

}

-(void)receiveSpecialityId:(NSNotification *)notificaton
{

    NSString *selectedServiceString=[[notificaton userInfo] valueForKey:@"service"];

    _specialtiyId = selectedServiceString;

    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"service" object:nil ];
}

I am trying to pass value from AViewController to BViewController

I have read all the discussions done previously on this same issue, but none of them solved my issue

Nitin Gohel
  • 49,482
  • 17
  • 105
  • 144
Developer
  • 822
  • 9
  • 23
  • 1
    Check please that your `BViewCOntroller.m` initialized before `AViewController.m` – iSashok Jun 17 '16 at 12:58
  • It is possible that the notification is sent before the new controller reaches the viewWillAppear. Can you post the method used to sent the notification and the code where you load the 2 controllers? – Marco Pace Jun 27 '16 at 16:03

1 Answers1

0

Dont' add

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(receiveSpecialityId:) name:@"speciality" object:nil];

in the viewDidAppear. I don't know much objective c but here's the deal in swift:

func receiveSpecialityID(notification: NSNotification){

  NSNotificationCenter.defaultCenter().addObserver(self, selector: #selector(<Theclassinwhichthismethodiswritten without the ()).receiveSpecialityID(_:)), name: "Selected", object: nil)   
 /// DO your thing here..
}

Then do a:

deinit{
NSNotificationCenter.DefaultCenter().removeObserver

}

This should work. Try and let me know!

Mtoklitz113
  • 3,828
  • 3
  • 21
  • 40