Instance code for the Lglalertview prompt box in IOS _ios

Source: Internet
Author: User
Tags uikit

Use and iOS8 later, just put the system Uialertcontroller encapsulation, save every time to write a lot of code. After encapsulation only need a code, deome address

: Https://github.com/liguoliangiOS/LGLAlertView.git

On code LGLAlertView.h:

#import <Foundation/Foundation.h> #import <UIKit/UIKit.h> typedef ns_enum (Nsinteger,
Lglalertviewactionstyle) {lglalertviewactionstyledefault = 0, Lglalertviewactionstylecancel,
Lglalertviewactionstyledestructive};
/** Alertview Callback block */typedef void (^callbackblock) (Nsinteger btnindex);
/** Alertview Callback block */typedef void (^textfieldcallbackblock) (NSString * text); @interface lglalertview:nsobject/** * Single or no buttons do nothing just prompt always use * @param title * @param message hint information * @param btnti Tle a single button with the title name * */+ (void) Showalertviewwith: (Uiviewcontroller *) Viewcontroller title: (NSString *) title message: (
NSString *) message buttontitle: (NSString *) btntitle ButtonStyle: (Lglalertviewactionstyle) ButtonStyle; /** * has two or more buttons to determine cancellation * @param title @param message tip * @param btntitle single button title name * @param cancelbuttontitle Cancel Button * @param destructivebtn destructivebtn button * @param otherbuttontitles OK button * * + (void) Showalertviewwith: (Uiviewcontrolle R *) Viewcontroller title: (Nsstring *) title message: (NSString *) message Callbackblock: (callbackblock) TextBlock cancelbuttontitle: (NSString *) Cancelbtntitle destructivebuttontitle: (NSString *) destructivebtntitle otherbuttontitles: (NSString *) otherBtnTitles ,...
Ns_requires_nil_termination; /** * There is an input box to determine the cancellation (note: There is only one input box) * @param title @param message Tip * @param btntitle the title name of a single button * @param cancelbu Ttontitle Cancel button * @param destructivebtn destructivebtn button * @param otherbuttontitles OK button * * + (void) showalerttextfieldvieww ITH: (Uiviewcontroller *) Viewcontroller title: (NSString *) title message: (NSString *) message Textfeildcallbackblock: ( Textfieldcallbackblock) block Cancelbuttontitle: (NSString *) cancelbtntitle otherbuttontitles: (NSString *)
Otherbtntitle; /** * Single or no button actionsheet just hint action button does not respond to event * @param title @param message Tip * @param btntitle the title 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 @param message Tip * @param btntitle the title of a single button * * * * */+ (void) Showaler
Tactionsheetwith: (Uiviewcontroller *) Viewcontroller title: (NSString *) title message: (NSString *) Callbackblock: (callbackblock) block Destructivebuttontitle: (NSString *) Destructivebtntitle CancelButtonTitle: ( NSString *) Cancelbtntitle otherbuttontitles: (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:uialertcontrollerstylealert]; if (btntitle.length) {Uialertactionstyle Actionstyle = (ButtonStyle = = Lglalertviewactionstyledefault)? Uialertactionstyledefault: ((ButtonStyle = = lglalertviewactionstylecancel)?
uialertactionstylecancel:uialertactionstyledestructive); Uialertaction * alertaction = alertaction = [uialertaction actionwithtitle:btntitle style:actionstyle-handler:^ ( Uialertaction * _nOnnull action {[Self performselector: @selector (dismissalertcontroller:) Withobject:alertcontroller Afterdelay:
Lglalertshowtime];
}];;
[Alertcontroller addaction:alertaction];
[Viewcontroller Presentviewcontroller:alertcontroller Animated:yes Completion:nil];
else {[Viewcontroller presentviewcontroller:alertcontroller animated:yes Completion:nil];
[Self performselector: @selector (dismissalertcontroller:) Withobject:alertcontroller afterdelay:lglalertshowtime];} ///single or multiple buttons + (void) Showalertviewwith: (Uiviewcontroller *) Viewcontroller title: (NSString *) title message: (NSString *) Message Callbackblock: (callbackblock) block Cancelbuttontitle: (NSString *) Cancelbtntitle Destructivebuttontitle: ( NSString *) Destructivebtntitle otherbuttontitles: (NSString *) Otherbtntitles,... {Uialertcontroller * Alertcontroller = [Uialertcontroller alertcontrollerwithtitle:title message:message
Preferredstyle:uialertcontrollerstylealert]; Add button if (cancelbtntitle.length) {uialertaction * cancelaction = [UIalertaction actionwithtitle:cancelbtntitle style:uialertactionstylecancel handler:^ (UIAlertAction * _Nonnull action
{block (0);}];
[Alertcontroller addaction:cancelaction]; } if (destructivebtntitle.length) {uialertaction * destructiveaction = [uialertaction actionwithtitle:
Destructivebtntitle style:uialertactionstyledestructive handler:^ (uialertaction * _Nonnull action) {block (1);}];
[Alertcontroller addaction:destructiveaction]; } if (otherbtntitles.length) {uialertaction *otheractions = [uialertaction actionwithtitle:otherbtntitles style: Uialertactionstyledefault 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:nil]; If there are no buttons, automatic delay disappears if (!cancelbtntitle.length &&!destructivebtntitle.length &&!otherbtntitles) {// At this point, self refers to this class [self performselector: @selector (dismissalertcontroller:) Withobject:alertcontroller Afterdelay:
Lglalertshowtime]; }//has input box + (void) Showalerttextfieldviewwith: (Uiviewcontroller *) Viewcontroller title: (NSString *) title message: ( NSString *) Message TextFeildcallbackblock: (textfieldcallbackblock) TextBlock cancelbuttontitle: (NSString *) cancelbtntitle Otherbuttontitles: (NSString *) otherbtntitle {Uialertcontroller * Alertcontroller = [Uialertcontroller
Alertcontrollerwithtitle:title Message:message Preferredstyle:uialertcontrollerstylealert]; [Alertcontroller addtextfieldwithconfigurationhandler:^ (Uitextfield *textfield)
{
}]; if (cancelbtntitle.length) {uialertaction * cancelaction = [uialertaction actionwithtitle:cancelbtntitle style:
Uialertactionstylecancel handler:^ (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 there are no buttons, automatic delay disappears if (!cancelbtntitle.length &&!otherbtntitle.length) {//At this point, self refers to this class [self performselector:@
Selector (dismissalertcontroller:) Withobject:alertcontroller afterdelay:lglalertshowtime]; }//========================================================================-----Alertview End-----============ ====================================================================== #pragma mark = = Click event = = = = + (void) Dismissalertcontroller: (Uialertcontroller *) 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:uialertcontrollerstyleactionsheet]; if (btntitle.length) {Uialertactionstyle Actionstyle = (ButtonStyle = = Lglalertviewactionstyledefault)? Uialertactionstyledefault: ((ButtonStyle = = lglalertviewactionstylecancel)?
uialertactionstylecancel:uialertactionstyledestructive); Uialertaction * alertaction = alertaction = [uialertaction actionwithtitle:btntitle style:actionstyle-handler:^ ( Uialertaction * _nonnull action) {[Self performselector: @selector (dismissalertcontroller:) Withobject:
Actioncontroller Afterdelay:lglalertshowtime];
}];;
[Actioncontroller addaction:alertaction];
[Viewcontroller Presentviewcontroller:actioncontroller Animated:yes Completion:nil]; else {[Viewcontroller presentviewcontroller:actioncontroller animated:yes Completion:nil];//If there are no buttons, automatic delay disappears [self
Performselector: @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 button if (destructivebtntitle.length) {uialertaction *destructiveaction = [uialertaction actionwithtitle:
Destructivebtntitle style:uialertactionstyledestructive handler:^ (uialertaction * _Nonnull action) {block (0);}];
[Actioncontroller addaction:destructiveaction]; } if (cancelbtntitle.length) {uialertaction *cancelaction = [uialertaction actionwithtitle:cancelbtntitle style:
Uialertactionstylecancel handler:^ (uialertaction *action) {destructivebtntitle.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 are no buttons, automatic delay disappears if (!cancelbtntitle.length &&!destructivebtntitle.length &&!otherbtntitles.length) {//At this point, self refers to this class [self performselector: @selector (dismissalertcontroller:) Withobject:actioncontroller Afterdelay:
Lglalertshowtime]; }} @end

The above is a small set up to introduce the iOS lglalertview hint box example code, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

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.