1

Is it possible to create a function which can generate a UIAlertAction and return it with handler.

I just don't want to write code for UIAlertAction multiple times, only trying to create single function which can create the UIAlertAction for every required scenario. Here is my code.

   UIAlertAction *actionPast = [self createActionButton:@"Past"];
   UIAlertAction *actionFuture = [self createActionButton:@"Future"];

   -(UIAlertAction *)createActionButton : (NSString *)title{
       UIAlertAction *action = [UIAlertAction actionWithTitle:title style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {}];
       return action;
    }

So is there any possibility to revert handler and perform any task on click of any UIAlertAction.

NSNoob
  • 5,548
  • 6
  • 41
  • 54
Sumit Jangra
  • 651
  • 5
  • 16

1 Answers1

1

Yes you can do that. Create the following method in your utility class/shared class (Whether you want it to be a class method or an instance method would depend on you):

+(UIAlertAction *) createAlertActionWithSelector:(SEL) selector andTitle:(NSString *) title andAlertActionStyle:(UIAlertActionStyle) style andCallBackTarget:(id) target{
    UIAlertAction *action;

    action = [UIAlertAction actionWithTitle:title style:style handler:^(UIAlertAction * action) {
        [target performSelector:selector];

    }];
    return action;
}

For example, if you wanted to create an AlertAction with title "OK", code embedded in method "okButtonTapped" and UIAlertActionStyleDefault, you'd call it like this:

UIAlertAction * okAction = [UtilityClass createAlertActionWithSelector:@selector(okButtonTapped)
                                                                     andTitle:@"OK"
                                                          andAlertActionStyle:UIAlertActionStyleDefault
                                                         andCallBackTarget:self];

Here CallbackTarget is whatever class you are calling your shared method from so we are passing self here. The method to be performed in handler must exist within the callBackTargetClass.

Now add it to your UIAlertController like:

[alertController addAction:okAction];

Just make sure to create methods for the code you want to run in the handler block and pass that method as selector to your alertAction creator method. That's it.

Disclaimer: It works but it is currently showing a warning that selector might cause a leak as selector is unknown on performing selector. See this answer for that warning

Community
  • 1
  • 1
NSNoob
  • 5,548
  • 6
  • 41
  • 54
  • thanks for the answer, it's working but the problem is i want to reduce code but with this approach the number of statements are same. – Sumit Jangra Nov 17 '16 at 10:45
  • So could you suggest me to follow this singleton approach because my code in Number of lines to create UIAlertAction not reduce. – Sumit Jangra Nov 17 '16 at 10:46
  • @SumitDhariwal Wait what? You should put this in a utility/singleton class and call this method from there as fits your scenarios. – NSNoob Nov 17 '16 at 10:51
  • 1
    Your question was to create a method which could generate UIAlertAction and its handler. Thats what this code does. How you use this method, whether in utility class or singleton class, is purely upto you. If you can't reduce the code of lines with this....Well you know the saying about water and horses. – NSNoob Nov 17 '16 at 10:53