0

I have an app that consists of several view controllers, in each view controllers I could load a UIAlertView.

What would be the best way of having a global alert view function? So I could effectively have a function bit like

[GlobalAlertVIew alertview : 2]; 

the 2 referring to what alert it is (bit like a enum).

I have tried adding an NSObject file and calling that, however on the return function

- (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex

how do I get from this file in the NSObject to the original view controller?

Thanks

Julian
  • 9,299
  • 5
  • 48
  • 65
Display Name
  • 1,025
  • 2
  • 15
  • 34
  • `how do I get from this file in the NSObject to the original view controller` what do you mean by this? – Julian Aug 17 '14 at 19:23
  • i can whack the UIAlertView call into a NSObject and it works fine, issue being the return, each ViewController has its own function on the - (void) alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex how do i get from the NSObject to my original view controller to carry out the relevant function – Display Name Aug 17 '14 at 19:26

3 Answers3

1

You either need to configure the delegate on the configured UIAlertView

UIAlertView *alertView = [GlobalAlertView alertView:2];
alertView.delegate = self;

or expose the delegate in the factory method

[GlobalAlertView alertView:2 withDelegate:self];
Paul.s
  • 38,494
  • 5
  • 70
  • 88
0

I don`t know if you still need, but I did like this:

+ (void)alert: (UIViewController *) view title: (NSString *) title message: (NSString *) message;

+ (void)alert: (UIViewController *) view title: (NSString *) title message: (NSString *) message {
    UIAlertController * alert=   [UIAlertController
                              alertControllerWithTitle:title
                              message:message
                              preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction* defaultAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault
                                                      handler:^(UIAlertAction * action) {}];

    [alert addAction:defaultAction];
    [view presentViewController:alert animated:YES completion:nil];
}
Matheus Weber
  • 194
  • 2
  • 3
  • 14
0

You can add the following code to your constants header file and import this header at .pch file to be able to see this file at app ,

#define SHOW_ALERT(title,msg){ UIAlertController *noDataFoundAlert = [UIAlertController alertControllerWithTitle:title message:msg preferredStyle:UIAlertControllerStyleAlert];UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action){}]; [noDataFoundAlert addAction:okAction]; [self presentViewController:noDataFoundAlert animated:YES completion:nil];}

And you can call this method at any class like the following call ,

 SHOW_ALERT(@"Please select any section", @"Please select any section");

Nada Gamal

Nada Gamal
  • 382
  • 4
  • 14