0

I am trying to change the views in a splitview controller based upon clicking a button in a modalview (a person is selecting an option). I am using notifications to accomplish this:

  1. When the button is clicked in the modal view, it issues a notice, then closes (dismisses) itself:

    [[NSNotificationCenter defaultCenter] postNotificationName:@"launchProject" object:nil];

  2. The DetailViewController inside the split view controller is listening for this notification, and switches out the views in the SVC

-(void)launchProject:(NSNotification *)notification {
    Project* secondDetail2 = [[Project alloc] initWithNibName:nil bundle:nil];
    ProjectRootController* secondRoot2 = [[ProjectRootController alloc] initWithNibName:nil bundle:nil ]; 
    self.splitViewController.viewControllers =[NSArray arrayWithObjects: secondRoot2, secondDetail2 , nil];

}

I don't understand why the views aren't switching out. Any advice on this will be welcome.

skaffman
  • 398,947
  • 96
  • 818
  • 769
Aaron
  • 605
  • 1
  • 9
  • 19

1 Answers1

0

You haven't shown all the code, so I'm guessing the problem is a misunderstanding of how notifications work. It can be initially confusing, but it's very straightforward. So far, you have:

[[NSNotificationCenter defaultCenter] postNotificationName:@"launchProject" object:nil]

which is fine.

But you also need to have

[[NSNotificationCenter defaultCenter]
 addObserver:self 
 selector:@selector(launchProject:) // selector should be your function name, launchProject
 name:@"launchProject" // notification name - must be same as what is given to postNotificatioName.
 object: nil];

somewhere, like in an init function.

In other words, postNotificationName:@"launchProject" does NOT call your function launchProject. It puts a notification with the name "launchProject" into the NSNotificationCenter defaultCenter. If you're not looking for that particular notification, then nothing will happen.

Hope that helps..

aqua
  • 3,269
  • 28
  • 41
  • Quite helpful. Thanks for your time. – Aaron Jan 21 '11 at 19:42
  • @Aaron, you can accept answers by clicking the green checkmark outline to the left of the answer. Please consider reviewing this answer and answers on your other questions and clicking the "accept" checkmark if applicable. – jball Jun 21 '11 at 16:40