3

I have more page will show the UIAlertController.

So I need write the alert code in a method and class in one page.

I want to use the class and call the method can show the alert on the any Viewcontroller .

How can I write presentviewcontroller in the class.

my header file is below:

 #import <Foundation/Foundation.h>
 #import "VisionAPI.h"

 @interface VisionAPI : NSObject

 +(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done;

 @end

My implement file is below:

 #import "VisionAPI.h"
 #import <UIKit/UIKit.h>
 @implementation VisionAPI

 +(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done{

     UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done  style:UIAlertActionStyleDefault
                                                       handler:nil];
       [showMsgAlertController addAction:showMsgAlertControllerOkAction];
dispatch_async(dispatch_get_main_queue(), ^{

       [self presentViewController:showMsgAlertController animated:YES completion:nil];
     });
 }
 @end

But upper code will show the error in this line:

 [self presentViewController:showMsgAlertController animated:YES completion:nil];

How can I presentViewController in the NSObject, or how to fix the problem.

Krunal
  • 77,632
  • 48
  • 245
  • 261
dickfala
  • 3,246
  • 3
  • 31
  • 52

2 Answers2

0

Add view controller as a parameter argument in your function and pass the 'self' (instance/object of view controller) from which view controller, you want to present your alert controller.

+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done fromViewController: (UIViewController)viewController{

     UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert];
UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done  style:UIAlertActionStyleDefault
                                                       handler:nil];
       [showMsgAlertController addAction:showMsgAlertControllerOkAction];
dispatch_async(dispatch_get_main_queue(), ^{

       [viewController presentViewController:showMsgAlertController animated:YES completion:nil];
     });
 }
 @end

You can also use, root view controller of your application, if you don't want to pass view controller as a parameter argument of this function each time. As follows:

+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done{

         UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert];
    UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done  style:UIAlertActionStyleDefault
                                                           handler:nil];
           [showMsgAlertController addAction:showMsgAlertControllerOkAction];
    dispatch_async(dispatch_get_main_queue(), ^{

RootViewController *rootController = (RootViewController*)[[(AppDelegate*)
                                   [[UIApplication sharedApplication]delegate] window] rootViewController];

           [rootController presentViewController:showMsgAlertController animated:YES completion:nil];
         });
     }
@end
Krunal
  • 77,632
  • 48
  • 245
  • 261
0

You can get the topmost view controller in NSObject as shown below:

- (UIViewController*)topMostController
        {
            UIViewController *topController = [self rootViewController];

            while ([topController presentedViewController]) topController = [topController presentedViewController];

            //  Returning topMost ViewController
            return topController;
        }


+(void) showMessageAlert:(NSString *) title andMessage:(NSString*) msg andDoneMsg:(NSString*) done{

         UIAlertController *showMsgAlertController = [UIAlertController alertControllerWithTitle: title message: msg preferredStyle: UIAlertControllerStyleAlert];
    UIAlertAction *showMsgAlertControllerOkAction = [UIAlertAction actionWithTitle:done  style:UIAlertActionStyleDefault
                                                           handler:nil];
           [showMsgAlertController addAction:showMsgAlertControllerOkAction];
    dispatch_async(dispatch_get_main_queue(), ^{

           [[self topMostController] presentViewController:showMsgAlertController animated:YES completion:nil];
         });
     }
user3575114
  • 993
  • 7
  • 13