iOS 显示警报控制器对象
iOS show alert controller object
我有更多页面将显示 UIAlertController。
所以我需要将警报代码写在一个方法中,class写在一页中。
我想使用 class 并调用方法可以在任何 Viewcontroller 上显示警报。
如何在 class.
中编写 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,或者如何解决问题。
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
如果您不想每次都将视图控制器作为此函数的参数参数传递,您也可以使用应用程序的根视图控制器。如下:
+(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
你可以在NSObject中得到最顶层的view controller,如下图:
- (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];
});
}
我有更多页面将显示 UIAlertController。
所以我需要将警报代码写在一个方法中,class写在一页中。
我想使用 class 并调用方法可以在任何 Viewcontroller 上显示警报。
如何在 class.
中编写 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,或者如何解决问题。
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
如果您不想每次都将视图控制器作为此函数的参数参数传递,您也可以使用应用程序的根视图控制器。如下:
+(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
你可以在NSObject中得到最顶层的view controller,如下图:
- (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];
});
}