Skip to content

Instantly share code, notes, and snippets.

@jeremytregunna
Created August 8, 2014 20:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeremytregunna/142160146f4d912b2fd9 to your computer and use it in GitHub Desktop.
Save jeremytregunna/142160146f4d912b2fd9 to your computer and use it in GitHub Desktop.
// Notification name as a constant, keep things clean. You'll see why this is needed soon.
static NSString* const MyStopNotificationName = @"MyStopNotificationName";
// Somewhere in your code you've got a button...
[self.someButton addTarget:self action:@selector(startConnection) forControlEvents:UIControlEventTouchUpInside];
// Somewhere else you have this method implemented
- (void)startConnection
{
self.someButton.enabled = NO;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(stopConnection) name:MyStopNotificationName object:nil];
// It does the thing that is required to start your connection. Forget about setting connectionStarted, you don't need it.
}
- (void)stopConnection
{
// This is called when that notification is posted, and only then.
self.someButton.enabled = YES;
}
// Whenever you would have set the connectionStopped boolean, do this instead:
[[NSNotificationCenter defaultCenter] postNotificationName:MyStopNotificationName object:nil];
// Profit!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment