Katerina
Katerina

Reputation: 187

ios heredity with objective c, how to call a method and not repeat it

I started a new iOS application with objective-C. I have a method that it shows an alert box and I realize that I use it in all my ViewControllers. What is the best way to implement this method so I don't have to repeat it? Should I create another view controller and import it on all the others? Is there a different type of class?

    - (void) alertStatus:(NSString *)msg :(NSString *)title
 {
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:title
                                                    message:msg
                                                   delegate:self
                                          cancelButtonTitle:@"Ok"
                                          otherButtonTitles:nil, nil];

[alertView show];
}

Upvotes: 0

Views: 141

Answers (4)

David Ansermot
David Ansermot

Reputation: 6112

You have multiple solutions :

  1. Create a viewController with this method and make your other view controllers inherit from it.
  2. Create a static inline function like showAlertStatus(NSString *msg, NSString *title) {...}
  3. Create a helper class with a class method +(void)... and call it something suitable such as: [MyHelper alertStatus:..]

Upvotes: 1

Daniel Galasko
Daniel Galasko

Reputation: 24247

There are two solutions to go about doing this.

Option One

Create a distinct class that is responsible for presenting alerts.

So something like ABCAlertViewPresenter. Then inside this class you could add your code to perform the presentation. Something akin to -[ABCAlertViewPresenter presentAlertWithStatus:title:]

The benefits to this approach is that you can abstract your alert presentation from your view controllers which should also make the transition to UIAlertController easier since UIAlertView is deprecated:

if ([UIAlertController class]) {
        //show an alert controller
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:alertTitle
                                                                       message:alertMessage
                                                                preferredStyle:UIAlertControllerStyleAlert];
        [viewController presentViewController:alert animated:YES completion:nil];
    } else {
        UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:alertTitle
                                                            message:alertMessage
                                                           delegate:nil
                                                  cancelButtonTitle:nil
                                                  otherButtonTitles:nil];
        [alertView show];
    }

Option Two

Move all your presentation and navigation code into a Navigation Coordinator Class. This removes the dependencies we tend to create between view controllers and their presented view controller counterparts. If you have ever called self.navigationController pushViewController... inside a UIViewController then you have already added dependencies between controllers. This makes it fairly difficult to customize animation presentations or even add new view controllers.

Then what you can do is either create a default base UIViewController and expose the coordination controller (which can either be a singleton or a property on your app delegate or even a property on your own subclass of UINavigationController). Then when you want to present an alert or a controller you ask your coordinator:

[self.coordinationController presenAlertViewFromViewController:self 
                                                         title:AlertTitle 
                                                       message:alertMessage];

Upvotes: 0

Ravi
Ravi

Reputation: 2451

try this... we can do this by using NSNotification

in your one of ViewController classes.. for ex: FirstViewController.m add the following code

- (void)viewDidLoad
{
    [super viewDidLoad];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(alertStatus:) name:@"ShowAlert" object:nil];

 }

-(void)alertStatus:(NSNotification*)sender
{
    UIAlertView * alert = [[UIAlertView alloc]initWithTitle:[sender.userInfo objectForKey:@"title"]
                                                    message:[sender.userInfo objectForKey:@"message"]
                                                   delegate:self
                                          cancelButtonTitle:@"ok"
                                          otherButtonTitles:nil];
    [alert show];
}

and in an other classes where you want to show alert... for ex : AnotherViewController.m call the notification like this..

[[NSNotificationCenter defaultCenter] postNotificationName:@"ShowAlert" object:nil userInfo:@{@"title":@"Alert1",@"message":@"This is first alert"}];

Upvotes: 0

bartl
bartl

Reputation: 392

Add category for UIAlertView with this method

Upvotes: 1

Related Questions