Implements simple encapsulation of the UIAlertController and UIAlertView after determining the system

Source: Internet
Author: User

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!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.