LGLAlertView prompt box and lglalertview prompt box
After iOS8 is used, the system's UIAlertController is encapsulated, saving a lot of code to be written every time you use it. After encapsulation, you only need a line of code, deome address: https://github.com/liguoliangiOS/LGLAlertView.git
Code LGLAlertView. h:
# Import <Foundation/Foundation. h> # import <UIKit/UIKit. h> typedef NS_ENUM (NSInteger, LGLAlertViewActionStyle) {LGLAlertViewActionStyleDefault = 0, callback, callback};/** alertView callback block */typedef void (^ CallBackBlock) (NSInteger btnIndex ); /** callback block of alertView */typedef void (^ TextFieldCallBackBlock) (NSString * text); @ interface LGLAlertView: NSObject/*** no operation is performed for a single button or no button, but the system prompts the title prompted by * @ param title * @ param message * @ param btnTitle the title of a single button * */+ (void) showAlertViewWith :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message buttonTitle :( NSString *) btnTitle buttonStyle :( LGLAlertViewActionStyle) buttonStyle; /*** there are two or more buttons to cancel the title * @ param message prompt information * @ param btnTitle the title name of a single button * @ param cancelButtonTitle cancel button * @ param destructiveBtn button * @ param otherButtonTitles OK button */+ (void) warning :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message CallBackBlock :( CallBackBlock) textBlock cancelButtonTitle :( NSString *) cancelBtnTitle destructiveButtonTitle :( NSString *) Warning warning :( NSString *) otherBtnTitles ,... NS_REQUIRES_NIL_TERMINATION;/*** if you have an input box, cancel it. (Note: Only one input box is provided here) * @ param title * @ param message * @ param btnTitle the title of a single button * @ param cancelButtonTitle cancel button * @ param destructiveBtn button * @ param otherButtonTitles OK button */+ (void) warning :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message TextFeildCallBackBlock :( TextFieldCallBackBlock) block cancelButtonTitle :( NSString *) cancelBtnTitle warning :( NSString *) otherBtnTitle; /*** single or no-button ActionSheet only prompts the action button and does not respond to the event * @ param title indicates the title * @ param message indicates the message * @ param btnTitle indicates the title name of a single button * */+ (void) showAlertActionSheetViewWith :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message buttonTitle :( NSString *) btnTitle buttonStyle :( LGLAlertViewActionStyle) buttonStyle; /*** no button ActionSheet button has a response event * @ param title indicates the title * @ param message indicates the message * @ param btnTitle indicates the title of a single button **/+ (void) warning :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message callbackBlock :( CallBackBlock) block destructiveButtonTitle :( NSString *) inclucancelbuttontitle :( NSString *) cancelBtnTitle failed :( NSString *) otherBtnTitles ,... NS_REQUIRES_NIL_TERMINATION; @ end
LGLAlertView. m: File
# Import "LGLAlertView. h "# define LGLAlertShowTime 1.0 @ implementation LGLAlertView // ====================== ========================================================== ====----- AlertView start ----- ====================================== ========================================================== ============/// single or no button + (void) showAlertViewWith :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message buttonTitle :( NSString *) btnTitle buttonStyle :( LGLAlertViewActionStyle) buttonStyle {UIAlertController * alertController = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: callback]; if (btnTitle. length) {UIAlertActionStyle actionStyle = (buttonStyle = LGLAlertViewActionStyleDefault )? UIAlertActionStyleDefault: (buttonStyle = LGLAlertViewActionStyleCancel )? Response: callback); UIAlertAction * alertAction = [UIAlertAction actionWithTitle: btnTitle style: actionStyle handler: ^ (UIAlertAction * _ Nonnull action) {[self resume mselector: @ selector (dismissAlertController :) withObject: alertController afterDelay: LGLAlertShowTime] ;}]; [alertController addAction: alertAction]; [viewController presentViewControl Ler: alertController animated: YES completion: nil];} else {[viewController presentViewController: alertController animated: YES completion: nil]; [self defined mselector: @ selector (dismissAlertController :) withObject: alertController afterDelay: LGLAlertShowTime] ;}// single or multiple buttons + (void) showAlertViewWith :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message CallBackBlock :( Cal LBackBlock) block cancelButtonTitle :( NSString *) cancelBtnTitle destructiveButtonTitle :( NSString *) destructiveBtnTitle otherButtonTitles :( NSString *) otherBtnTitles ,... {UIAlertController * alertController = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: UIAlertControllerStyleAlert]; // Add the button if (cancelBtnTitle. length) {UIAlertAction * cancelAction = [UIAlertAction CtionWithTitle: cancelBtnTitle style: UIAlertActionStyleCancel handler: ^ (UIAlertAction * _ Nonnull action) {block (0) ;}]; [alertController addAction: cancelAction];} if. length) {UIAlertAction * destructiveAction = [UIAlertAction actionWithTitle: destructiveBtnTitle style: Invalid handler: ^ (UIAlertAction * _ Nonnull action) {block (1) ;}]; [alertController add Action: destructiveAction];} if (otherBtnTitles. length) {UIAlertAction * otherActions = [UIAlertAction actionWithTitle: Custom style: Custom handler: ^ (UIAlertAction * action ){(! CancelBtnTitle. length &&! DestructiveBtnTitle. length )? Block (0): (cancelBtnTitle. length &&! DestructiveBtnTitle. length) | (! CancelBtnTitle. length & destructiveBtnTitle. length ))? Block (1): block (2);}]; [alertController addAction: otherActions]; va_list args; va_start (args, otherBtnTitles); if (otherBtnTitles. length) {NSString * otherString; int index = 2 ;(! CancelBtnTitle. length &&! DestructiveBtnTitle. length )? (Index = 0): (cancelBtnTitle. length &&! DestructiveBtnTitle. length) | (! CancelBtnTitle. length & destructiveBtnTitle. length )? (Index = 1): (index = 2); while (otherString = va_arg (args, NSString *) {index ++; UIAlertAction * otherActions = [UIAlertAction actionWithTitle: otherString style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {block (index) ;}]; [alertController addAction: otherActions] ;}} va_end (args );} [viewController presentViewController: alertController animated: YES completion: ni L]; // if there is no button, the automatic delay disappears if (! CancelBtnTitle. length &&! DestructiveBtnTitle. length &&! OtherBtnTitles) {// at this time, self indicates the class [self specified mselector: @ selector (dismissAlertController :) withObject: alertController afterDelay: LGLAlertShowTime];} // input box + (void) authorization :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message TextFeildCallBackBlock :( TextFieldCallBackBlock) textBlock cancelButtonTitle :( NSString *) cancelBtnTitle otherButtonTitles :( NSStr Ing *) otherBtnTitle {UIAlertController * alertController = [UIAlertController failed: title message: message preferredStyle: Success]; [alertController failed: ^ (UITextField * textField) {}]; if (cancelBtnTitle. length) {UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: cancelBtnTitle style: UIAlertActionStyleCancel handle R: ^ (UIAlertAction * action) {[self dismissAlertController: alertController] ;}]; [alertController addAction: cancelAction];} if (otherBtnTitle. length) {UIAlertAction * otherAction = [UIAlertAction actionWithTitle: otherBtnTitle style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action) {textBlock ([alertController. textFields firstObject]. text) ;}]; [alertController addAction: otherAction];} [ViewController presentViewController: alertController animated: YES completion: nil]; // if no button exists, the automatic delay disappears if (! CancelBtnTitle. length &&! OtherBtnTitle. length) {// at this time, self indicates the class [self Checking mselector: @ selector (dismissAlertController :) withObject: alertController afterDelay: LGLAlertShowTime];} // ================================================ ========================================= ----- AlertView end ----- = ========================================================== ========================================================== # pragma mark === Click Event ======+ (void) dismissAlertController :( UIAle RtController *) alert {[alert dismissViewControllerAnimated: YES completion: nil];} // ================================================ =========================================-- ActionSheet Start -- = ========================================================== ========================================================== ==+ (void) showAlertActionSheetViewWith :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message buttonTitle :( NSString *) btnTitle buttonStyle :( LGLAlertViewActionStyle) buttonStyle {UIAlertController * actionController = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: callback]; if (btnTitle. length) {UIAlertActionStyle actionStyle = (buttonStyle = LGLAlertViewActionStyleDefault )? UIAlertActionStyleDefault: (buttonStyle = LGLAlertViewActionStyleCancel )? Response: callback); UIAlertAction * alertAction = [UIAlertAction actionWithTitle: btnTitle style: actionStyle handler: ^ (UIAlertAction * _ Nonnull action) {[self resume mselector: @ selector (dismissAlertController :) withObject: actionController afterDelay: LGLAlertShowTime] ;}]; [actionController addAction: alertAction]; [viewController presentViewContr Oller: actionController animated: YES completion: nil];} else {[viewController presentViewController: actionController animated: YES completion: nil]; // if no button exists, automatic delay disappears [self defined mselector: @ selector (dismissAlertController :) withObject: actionController afterDelay: LGLAlertShowTime] ;}+ (void) showAlertActionSheetWith :( UIViewController *) viewController title :( NSString *) title message :( NSString *) message CallbackBlock :( CallBackBlock) block destructiveButtonTitle :( NSString *) destructiveBtnTitle cancelButtonTitle :( NSString *) cancelBtnTitle otherButtonTitles :( NSString *) otherBtnTitles ,... {UIAlertController * actionController = [UIAlertController alertControllerWithTitle: title message: message preferredStyle: UIAlertControllerStyleActionSheet]; // Add the button if (destructiveBtnTitle. length) {UIAlertAction * d EstructiveAction = [UIAlertAction actionWithTitle: destructiveBtnTitle style: incluhandler: ^ (UIAlertAction * _ Nonnull action) {block (0) ;}]; [actionController addAction: destructiveAction];} if (cancelBtnTitle. length) {UIAlertAction * cancelAction = [UIAlertAction actionWithTitle: cancelBtnTitle style: UIAlertActionStyleCancel handler: ^ (UIAlertAction * action) {destructiveBtn Title. length? Block (1): block (0);}]; [actionController addAction: cancelAction];} if (otherBtnTitles. length) {UIAlertAction * otherActions = [UIAlertAction actionWithTitle: otherBtnTitles style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * action ){(! CancelBtnTitle. length &&! DestructiveBtnTitle. length )? Block (0): (destructiveBtnTitle. length &&! CancelBtnTitle. length) | (! DestructiveBtnTitle. length & cancelBtnTitle. length ))? Block (1): block (2);}]; [actionController addAction: otherActions]; va_list args; va_start (args, otherBtnTitles); if (otherBtnTitles. length) {NSString * otherString; int index = 2 ;(! CancelBtnTitle. length &&! DestructiveBtnTitle. length )? (Index = 0): (cancelBtnTitle. length &&! DestructiveBtnTitle. length) | (! CancelBtnTitle. length & destructiveBtnTitle. length )? (Index = 1): (index = 2); while (otherString = va_arg (args, NSString *) {index ++; UIAlertAction * otherActions = [UIAlertAction actionWithTitle: otherString style: UIAlertActionStyleDefault handler: ^ (UIAlertAction * _ Nonnull action) {block (index) ;}]; [actionController addAction: otherActions] ;}} va_end (args );} [viewController presentViewController: actionController animated: YES completion: Nil]; // if there is no button, the automatic delay disappears if (! CancelBtnTitle. length &&! DestructiveBtnTitle. length &&! OtherBtnTitles. length) {// at this time, self indicates the [self specified mselector: @ selector (dismissAlertController :) withObject: actionController afterDelay: LGLAlertShowTime];} @ end