IOS Notification Controller Object
I have more pages to show UIAlertController.
So I need to write the warning code in the method and class on the same page.
I want to use a class and the method call can show a warning on any Viewcontroller.
How can I write presentviewcontroller in a 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 implementation 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 the above code will show an error on this line:
[self presentViewController:showMsgAlertController animated:YES completion:nil];
How can I introduce the ViewController to NSObject or how to fix the problem.
source to share
Add the view controller as a parameter in your function and pass the "i" (instance / object of the view controller) from which controller you want to expose 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 your application's root view controller if you don't want to pass the view controller as a parameter argument to this function every time. As shown below:
+(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
source to share
You can get the top view controller in NSObject like 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];
});
}
source to share