Basic navigation between screens

How do I navigate between screens in a master detail scenario?

The user clicks on a row in the master view and wants to edit or just view the record in detail view.

The solution for this is not very hard to implement. The certain controllers communicate send messages via NSNotificationCenter like “show this in detail” or “navigate back”.

In this scenario, we have three controllers:

  • HomeController (Host for master and detail controller, does the actual view swapping)
  • MasterController (Shows all data in a list)
  • DetailController (Shows a certain record in detail)

The HomeController subscribes to the ShowDetail event:

-(void) awakeFromNib{
  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  [nc addObserver:self selector:@selector(showMessage:) name:@“ShowMessageDetail” object:nil];
}

-(void) showMessage: (NSNotification *) notification{
  id message = [[notification userInfo] valueForKey:@"message"];
  //Do the actual view swapping stuff here
}

In the MasterController, we react on double clicking a row in the table view:

-(void) tableViewDoubleClick: (id) sender{
  NSManagedObject *message = [[[self arrayController] arrangedObjects] objectAtIndex: [sender selectedRow]];
  NSDictionary *userInfo = [NSDictionary dictionaryWithObject:message forKey:@"message"];

  NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
  [nc postNotificationName:@"ShowMessageDetail" object:nil userInfo:userInfo];
}

gist

The special thing here is, that the userInfo dictionary holds the selected record from the table view. If you just want to notify the other one without any additional information, pass nil to userInfo.