When is it acceptable to reduce code duplication by placing code in an AppDelegate?

I have a very small Xcode project with multiple view controllers. I found that I duplicated the following methods in them:

- (void)postTip:(NSString *)message {
    [self postInfoAlertWithTitle:@"Tip" andMessage:message andAction:@"Got it!"];
}

- (void)postInfoAlertWithTitle:(NSString *)title andMessage:(NSString *)message andAction:(NSString *)action {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:action style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

      

Of course, this got me thinking about how I can remove (or at least reduce) the duplicated code.

The obvious answer is to put the desired behavior in a parent class and then inherit from that class for my view controllers. However, some of the view controllers are of type UICollectionViewController and some of them are of type UITableViewController, and I don't know how to store their collections and table attributes, respectively, in the event that I inherit from the hypothetical MyViewController. UIViewController.

So I researched a bit and looked at the protocols. Initially this seemed like a good help, except that you cannot provide a default implementation for methods declared in the protocol, which is basically what I would like.

Finally, I considered with great hesitation and self-denial the question of behavior in the AppDelegate class with an additional parameter to facilitate the presentation of warnings:

- (void)postTip:(NSString *)message toController:(UIViewController *)controller;
- (void)postInfoAlertWithTitle:(NSString *)title andMessage:(NSString *)message andAction:(NSString *)action toController:(UIViewController *)controller;

      

The call in this view manager looks like this:

[self.appDelegate postTip:@"Git gud!" toController:self];

      

This voila! The behavior I want is where I want it and all I have to do is get the AppDelegate instance! But ... that's not good with me. Seems ... smelly. Also, there is still some overlap, i.e. Declaring and initializing the private property appDelegate, which I took care of, rather than calling the (AppDelegate *) [[UIApplication sharedApplication] delegate] wherever I need it, so that:

  • I can specify "weak" and avoid possible save cycles
  • I only take one pointer to the AppDelegate (hurray for premature optimization>. <)

Is it acceptable to use AppDelegate as a repository for overall application behavior such as utility methods, and if so, am I unreasonably paranoid about implementing re: using a property? (If not, what are my options?)

+3


source to share


2 answers


Definitely use a category when creating .h and .m files

UIViewController + InfoAlert.h

@interface UIViewController (InfoAlert)

- (void)postInfoAlertWithTitle:(NSString *)title andMessage:(NSString *)message andAction:(NSString *)action;

@end

      



UIViewController + InfoAlert.m

#import "UIViewController+InfoAlert.h"

@implementation UIViewController (InfoAlert)

- (void)postInfoAlertWithTitle:(NSString *)title andMessage:(NSString *)message andAction:(NSString *)action {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:action style:UIAlertActionStyleDefault handler:nil]];
    [self presentViewController:alert animated:YES completion:nil];
}

@end

      

then just import your UIViewController + InfoAlert.h where you want to use the new postInfoAlertWithTitle method

+1


source


It seems to me that categories were designed for exactly this kind of situations, although as others have pointed out in the comments, there are additional options - and that's what I did.



To view documentation for the Apple category, see this page . To add a category to Xcode see this page .

0


source







All Articles