Skip to content

Instantly share code, notes, and snippets.

@christineyen
Created May 3, 2012 21:05
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christineyen/2589505 to your computer and use it in GitHub Desktop.
Save christineyen/2589505 to your computer and use it in GitHub Desktop.
Framework vs. BlocksKit handling, for blog
- (IBAction)clickedCloseButton:(id)sender {
UIActionSheet *actionSheet = [[UIActionSheet alloc] initWithTitle:@"Discard post?"
delegate:self
cancelButtonTitle:@"Cancel"
destructiveButtonTitle:@"Discard"
otherButtonTitles:@"Save draft", nil];
[actionSheet showInView:self.view];
}
// UIActionSheetDelegate methods
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex {
if (buttonIndex == 0) {
// save as draft
} else if (buttonIndex == 1) {
// discard
}
}
- (IBAction)clickedCloseButton:(id)sender {
UIActionSheet *confirmSheet = [UIActionSheet actionSheetWithTitle:@"Discard post?"];
[confirmSheet addButtonWithTitle:@"Save draft" handler:^{
// save as draft
}];
[confirmSheet setDestructiveButtonWithTitle:@"Discard" handler:^{
// discard
}];
[confirmSheet setCancelButtonWithTitle:@"Cancel" handler:NULL];
[confirmSheet showInView:self.view];
}
- (void)done:(id)sender {
[self dismissViewControllerAnimated:YES completion:NULL];
}
- (void)viewDidLoad {
// ... normal viewDidLoad setup
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
target:self
action:@selector(done:)];
}
- (void)viewDidLoad {
// ... normal viewDidLoad setup
UIViewController *__weak blockSelf = self;
self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone
handler:^(id sender) {
// Calling dismissViewControllerAnimated:completion: on nil
// is actually fine. However, if a nil blockSelf is *not*
// all right, do something like the following to nil-check
// (threadsafe-edly!) the previously-unretained copy of self:
//
// UIViewController *strongBlockSelf = blockSelf;
// if (strongBlockSelf) { // then do things }
[blockSelf dismissViewControllerAnimated:YES completion:NULL]; // safely avoids
}];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment