Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Block for UIAlertViewDelegate

Tags:

I'm pretty new to objective C and I'm just trying to figure out if I can use a block or a selector as the UIAlertViewDelegate argument for UIAlertView - and which is more appropriate?

I've tried the following but it just isn't working so I'm not sure if I'm on the right track or not?

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Checked In"      message:responseString     delegate:^(UIAlertView * alertView, NSInteger buttonIndex)                                                     {                                                        NSLog(@"Done!");                                                    }      cancelButtonTitle:@"OK"      otherButtonTitles: nil]; 

Thanks!

like image 408
jb007 Avatar asked Apr 10 '12 02:04

jb007


2 Answers

Great idea. Here it is. Just like alert view, except adds a block property that's invoked when the alert is dismissed. (Edit - I've simplified this code since the original answer. Here's what I use now in projects)

//  AlertView.h //  #import <UIKit/UIKit.h>  @interface AlertView : UIAlertView  @property (copy, nonatomic) void (^completion)(BOOL, NSInteger);  - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles;  @end  // //  AlertView.m  #import "AlertView.h"  @interface AlertView () <UIAlertViewDelegate>  @end  @implementation AlertView  - (id)initWithTitle:(NSString *)title message:(NSString *)message cancelButtonTitle:(NSString *)cancelButtonTitle otherButtonTitles:(NSArray *)otherButtonTitles {      self = [self initWithTitle:title message:message delegate:self cancelButtonTitle:cancelButtonTitle otherButtonTitles:nil];      if (self) {         for (NSString *buttonTitle in otherButtonTitles) {             [self addButtonWithTitle:buttonTitle];         }     }     return self; }  - (void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex {      if (self.completion) {         self.completion(buttonIndex==self.cancelButtonIndex, buttonIndex);         self.completion = nil;     } }  @end 

You can extend this idea to supply blocks for other delegate methods, but the didDismiss is the most common.

Call it like this:

AlertView *alert = [[AlertView alloc] initWithTitle:@"Really Delete" message:@"Do you really want to delete everything?" cancelButtonTitle:@"Nevermind" otherButtonTitles:@[@"Yes"]];  alert.completion = ^(BOOL cancelled, NSInteger buttonIndex) {     if (!cancelled) {         [self deleteEverything];     } }; [alert show]; 
like image 86
danh Avatar answered Sep 17 '22 19:09

danh


One must use UIAlertController for that approach as Apple document says

A UIAlertController object displays an alert message to the user. This class replaces the UIActionSheet and UIAlertView classes for displaying alerts. After configuring the alert controller with the actions and style you want, present it using the presentViewController:animated:completion: method.

In addition to displaying a message to a user, you can associate actions with your alert controller to give the user a way to respond. For each action you add using the addAction: method, the alert controller configures a button with the action details. When the user taps that action, the alert controller executes the block you provided when creating the action object. Apple Docs.

UIAlertController* alert = [UIAlertController alertControllerWithTitle:@"My Alert"          message:@"This is an alert."                                    preferredStyle:UIAlertControllerStyleAlert];      UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault        handler:^(UIAlertAction * action) {}];      [alert addAction:defaultAction];     [self presentViewController:alert animated:YES completion:nil]; 
like image 32
ZaEeM ZaFaR Avatar answered Sep 18 '22 19:09

ZaEeM ZaFaR