我有更多页面将显示UIAlertController。

因此,我需要在一页中的方法和类中编写警报代码。

我想使用该类并调用该方法可以在任何Viewcontroller上显示警报。

如何在该类中编写presentviewcontroller。

我的头文件如下:

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

 @interface VisionAPI : NSObject

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

 @end

我的工具文件如下:
 #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

但是上面的代码将在此行显示错误:
 [self presentViewController:showMsgAlertController animated:YES completion:nil];

我该如何在NSObject中呈现ViewController,或者如何解决该问题。

最佳答案

将视图控制器添加为函数中的参数实参并传递
哪个视图的“自身”(视图控制器的实例/对象)
控制器,您要显示警报控制器。

+(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

如果您不想每次都将视图控制器作为该函数的参数参数传递,则还可以使用应用程序的根视图控制器。如下:
+(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

10-06 05:28
查看更多