Skip to content

Instantly share code, notes, and snippets.

@mactive
Last active August 29, 2015 14:02
Show Gist options
  • Save mactive/f6f7c8aa2129425a1dc4 to your computer and use it in GitHub Desktop.
Save mactive/f6f7c8aa2129425a1dc4 to your computer and use it in GitHub Desktop.
Notification Center
// Instead of using delegation for passing the data, consider using NSNotificationCenter to communicate between your view controllers in this situation.
// In your first view controller you will register to listen for a notification:
- (void)viewDidLoad
{
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(handleFourthViewSubmit:)
name:@"fourthViewSubmit"
object:nil];
}
// And create the method to be run when the notification is sent:
- (void)handleFourthViewSubmit:(NSNotification *)notification {
NSDictionary *theData = [notification userInfo]; // theData is the data from your fourth view controller
// pop views and process theData
}
// In your first view controller's dealloc method, be sure to un-register as an observer (to avoid potential crashes):
-(void) dealloc {
[[NSNotificationCenter defaultCenter] removeObserver:self];
[super dealloc];
}
// Then in your fourth view controller, broadcast the notification when the enter button is pressed:
// note: dataDict should be an NSDictionary containing the data you want to send back to your first view controller
[[NSNotificationCenter defaultCenter] postNotificationName:@"fourthViewSubmit"
object:self
userInfo:dataDict];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment