Implements simple encapsulation of the UIAlertController and UIAlertView after determining the system
After iOS8, UIAlertController is used to replace UIAlertView. Therefore, when a window needs to pop up, you must first judge the system. There are many pop-up windows in the recently created project. If you judge every time, this is really too troublesome. We simply encapsulate UIAlertController and UIAlertView in a tool class, and then judge the system in the tool class, then you can directly call the method of this tool class on the interface you need to pop up the window, reducing code coupling.
In fact, this tool class is also very simple, because it is used by the system, share it for your reference:
First, the. h file
@interface UIAlertTool : NSObject-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle;;@end
There is only one simple way to pass in the content you need to display in the pop-up window as a parameter.
Then the implementation of the. m file
#define IAIOS8 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)#import "UIAlertTool.h"typedef void (^confirm)();typedef void (^cancle)();@interface UIAlertTool(){ confirm confirmParam; cancle cancleParam;}@end@implementation UIAlertTool-(void)showAlertView:(UIViewController *)viewController :(NSString *)title :(NSString *)message :(NSString *)cancelButtonTitle :(NSString *)otherButtonTitle :(void (^)())confirm :(void (^)())cancle{ confirmParam=confirm; cancleParam=cancle; if (IAIOS8) { UIAlertController *alertController = [UIAlertController alertControllerWithTitle:title message:message preferredStyle:UIAlertControllerStyleAlert]; // Create the actions. UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelButtonTitle style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { cancle(); }]; UIAlertAction *otherAction = [UIAlertAction actionWithTitle:otherButtonTitle style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) { confirm(); }]; // Add the actions. [alertController addAction:cancelAction]; [alertController addAction:otherAction]; [viewController presentViewController:alertController animated:YES completion:nil]; } else{ UIAlertView *TitleAlert = [[UIAlertView alloc] initWithTitle:title message:message delegate:self cancelButtonTitle:otherButtonTitle otherButtonTitles:cancelButtonTitle,nil]; [TitleAlert show]; } }-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{ if (buttonIndex==0) { confirmParam(); } else{ cancleParam(); }}@end
In the pop-up window, click OK or cancel the function to be implemented and write it into the block. SO easy!