iOS显示警报控制器对象

时间:2017-05-16 11:02:15

标签: ios objective-c swift xcode uialertcontroller

我有更多页面会显示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,或者如何解决问题。

2 个答案:

答案 0 :(得分:0)

  

在您的函数中添加视图控制器作为参数参数并传递   自我&#39; (视图控制器的实例/对象)从哪个视图   控制器,您想要提供警报控制器。

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

如果您不希望每次都将视图控制器作为此函数的参数参数传递,也可以使用应用程序的根视图控制器。如下:

<%= social_share_button_tag(@abc.full_name,
url: "#{@abc.page}?action=xyz&record=#{@abc.id}",
desc: @abc.full_name,
popup: 'true',
'data-facebook-title' => "The news of #{@abc.full_name}",
'data-twitter-title' => "The news of #{@abc.full_name}",
'data-google_plus-title' => "The news of #{@abc.full_name}",
'data-facebook-caption' => "The news of #{@abc.full_name}"

答案 1 :(得分:0)

您可以在NSObject中获取最顶层的视图控制器,如下所示:

- (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];
         });
     }