iOS 显示警报控制器对象
Posted
技术标签:
【中文标题】iOS 显示警报控制器对象【英文标题】:iOS show alert controller object 【发布时间】:2017-05-16 11:02:15 【问题描述】:我有更多页面会显示 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,或者如何解决问题。
【问题讨论】:
你需要视图控制器来呈现 alertviewcontroller。正如@Krunal 建议的那样 【参考方案1】:在你的函数中添加视图控制器作为参数参数并传递 来自哪个视图的“自我”(视图控制器的实例/对象) 控制器,你想展示你的警报控制器。
+(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
【讨论】:
【参考方案2】:你可以得到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];
);
【讨论】:
以上是关于iOS 显示警报控制器对象的主要内容,如果未能解决你的问题,请参考以下文章