Advertisements
Alert View Controller
Following provides some utility methods to create UIAlertViewControllers with completion block.
+ (void)showAlertWithTitle:(NSString *)title message:(NSString *)message presentingViewController:(UIViewController *)presentingViewController { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:^{ }]; }]; [alertController addAction:okAction]; [presentingViewController presentViewController:alertController animated:YES completion:^{ }]; } + (void)showAlertWithTitle:(NSString *)title message:(NSString *)message presentingViewController:(UIViewController *)presentingViewController cancelString:(NSString *)cancelString okString:(NSString *)okString completion:(void (^) (BOOL okClicked))completion { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; if (!okString) { okString = @"OK"; } UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(okString, @"OK action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:^{ completion(YES); }]; }]; if (cancelString) { UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:NSLocalizedString(cancelString, @"Cancel action") style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:^{ completion(NO); }]; }]; [alertController addAction:cancelAction]; } [alertController addAction:okAction]; [presentingViewController presentViewController:alertController animated:YES completion:^{ }]; } + (void)showAlertWithTitle:(NSString *)title message:(NSString *)message presentingViewController:(UIViewController *)presentingViewController cancelString:(NSString *)cancelString option1:(NSString *)option1 completion1:(void (^) ())completion1 option2:(NSString *)option2 completion2:(void (^) ())completion2 { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:^{ }]; }]; [alertController addAction:cancelAction]; if (option1) { UIAlertAction *action1 = [UIAlertAction actionWithTitle:option1 style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:^{ }]; completion1(); }]; [alertController addAction:action1]; } if (option2) { UIAlertAction *action2 = [UIAlertAction actionWithTitle:option2 style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { [alertController dismissViewControllerAnimated:YES completion:^{ }]; completion2(); }]; [alertController addAction:action2]; } [presentingViewController presentViewController:alertController animated:YES completion:^{ }]; }