iOS combat-using Block's Uialertview

Source: Internet
Author: User

Because block is more useful, you know, directly on the code:

. h

typedef void (^choicecompletionblock) (int index);
+  (instancetype) sharealertview; #pragma  mark -  pop-up//normal cue pop-up-  (void) Showtipalert: ( nsstring *) message;-  (void) Showtipalert: (nsstring *) Message completion: ( Choicecompletionblock) completion;//selectable pop-up window-  (void) Showchoicealert: (nsstring *) message  Completion: (choicecompletionblock) completion;-  (void) Showchoicealert: (nsstring *) message  Donetitle: (nsstring *) Donetitile completion: (choicecompletionblock) completion;-  (void) Showchoicealert: (nsstring *) Message button1title: (nsstring *) Title1 button2title: (NSString  *) Title2 completion: (choicecompletionblock) completion;//  customizable title-  (void) ShowChoiceAlert :(nsstring *) Message title: (nsstring *) Title donetitle: (nsstring *) doneTitle  Completion: (choicecompletionblock) completion;-  (void) Showchoicealert: (nsstring *) Message title :(nsstring *) Title button1title: (nsstring *) ButtOn1title button2title: (nsstring *) Button2title completion: (Choicecompletionblock) completion;// Three-choice pop-up window-  (void) Showchoicealert: (nsstring *) Message button1title: (nsstring *) title1  Button2title: (nsstring *) Title2 button3title: (nsstring *) Title3 completion: ( Choicecompletionblock) Completion;


. m

Choicecompletionblock _choicecompletion;
-  (void) Showtipalert: (nsstring *) message{    uialertview *alert =  [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:nil  cancelbuttontitle:@ "OK"  otherbuttontitles:nil];    [alert show];} -  (void) Showtipalert: (nsstring *) Message completion: (Choicecompletionblock) completion{     if  (_choicecompletion)  {        return;     }        uialertview *alert = [ [uialertview alloc] initwithtitle:tiptitle message:message delegate:self  cancelbuttontitle:@ "OK"  otherButtonTitles:nil];    [alert show];         _choicecompletion = [completion copy];} -  (void) Showchoicealert: (nsstring *) Message completion: (CHOICECOmpletionblock) completion{    [self showchoicealert:message donetitle:@ "OK"  completion:completion];} -  (void) Showchoicealert: (nsstring *) Message donetitle: (nsstring *) donetitile  Completion: (Choicecompletionblock) completion{    [self showchoicealert:message  Title:tiptitle donetitle:donetitile completion:completion];} -  (void) Showchoicealert: (nsstring *) Message title: (nsstring *) Title donetitle: ( nsstring *) Donetitle completion: (Choicecompletionblock) completion {    if   (_choicecompletion)  {        return;     }        UIAlertView *alert = [[UIAlertView  alloc] initwithtitle:title message:message delegate:self cancelbuttontitle:@ "Cancel"   otherbuttontitles:donetitle, nil];    [alert show];    _choicecompletion = [completion copy];} -  (void) Showchoicealert: (nsstring *) Message button1title: (nsstring *) title1  Button2title: (nsstring *) Title2 completion: (Choicecompletionblock) completion{     if  (_choicecompletion)  {        return;     }        uialertview *alert = [[uialertview  alloc] initwithtitle:tiptitle message:message delegate:self cancelbuttontitle:nil  otherButtonTitles:title1, title2, nil];    [alert show];     _choicecompletion = [completion copy];} -  (void) Showchoicealert: (nsstring *) Message title: (nsstring *) Title button1title: ( nsstring *) Button1title button2title: (nsstring *) Button2title&nbsP;completion: (Choicecompletionblock) completion {    if  (_choiceCompletion)   {        return;    }         uialertview *alert = [[uialertview alloc] initwithtitle:title  message:message delegate:self cancelButtonTitle:nil otherButtonTitles:button1Title,  Button2title, nil];    [alert show];    _choicecompletion  = [completion copy];} -  (void) Showchoicealert: (nsstring *) Message button1title: (nsstring *) title1  Button2title: (nsstring *) Title2 button3title: (nsstring *) Title3 completion: ( Choicecompletionblock) completion{    if  (_choicecompletion)  {         return;    }         UialeRtview *alert = [[uialertview alloc] initwithtitle:tiptitle message:message  delegate:self cancelButtonTitle:nil otherButtonTitles:title1, title2, title3,  nil];    [alert show];    _choicecompletion = [ Completion copy];}
#pragma mark-uialertviewdelegate-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger)        buttonindex{if (_choicecompletion) {_choicecompletion (buttonindex);    _choicecompletion = nil; }}

The above code will have a problem, that is, if the code calls alert multiple times can only show one, the solution I think of a good.

#import <objc/runtime.h> #define Eocmyalertviewkey @ "Eocmyalertviewkey"
-  (void) Showtipalert: (nsstring *) message{    uialertview *alert =  [[UIAlertView alloc] initWithTitle:TIPTITLE message:message delegate:nil  cancelbuttontitle:@ "OK"  otherbuttontitles:nil];    [alert show];} -  (void) Showtipalert: (nsstring *) Message completion: (Choicecompletionblock) completion{     uialertview *alert = [[uialertview alloc] initwithtitle:tiptitle  message:message delegate:self cancelbuttontitle:@ "OK"  otherButtonTitles:nil];     objc_setassociatedobject (Alert, eocmyalertviewkey, completion, objc_association_ COPY);    [alert show];    }-  (void) ShowChoiceAlert: ( nsstring *) Message completion: (Choicecompletionblock) completion{    [self  showchoicealert:message donetitle:@ "OK"  comPletion:completion];} -  (void) Showchoicealert: (nsstring *) Message donetitle: (nsstring *) donetitile  Completion: (Choicecompletionblock) completion{    [self showchoicealert:message  Title:tiptitle donetitle:donetitile completion:completion];} -  (void) Showchoicealert: (nsstring *) Message title: (nsstring *) Title donetitle: ( nsstring *) Donetitle completion: (Choicecompletionblock) completion {     Uialertview *alert = [[uialertview alloc] initwithtitle:title message:message  delegate:self cancelbuttontitle:@ "Cancel"  otherButtonTitles:doneTitle, nil];     objc_setassociatedobject (Alert, eocmyalertviewkey, completion, objc_association_ COPY);     [alert show];} -  (void) Showchoicealert: (nsstring *) Message button1title: (nsstring *) title1  Button2title: (nsstring *) Title2 completion: (Choicecompletionblock) completion{    uialertview * Alert = [[uialertview alloc] initwithtitle:tiptitle message:message delegate: self cancelbuttontitle:nil otherbuttontitles:title1, title2, nil];     Objc_setassociatedobject (alert, eocmyalertviewkey, completion, objc_association_copy);     [alert show];} -  (void) Showchoicealert: (nsstring *) Message title: (nsstring *) Title button1title: ( nsstring *) Button1title button2title: (nsstring *) Button2title completion: ( Choicecompletionblock) completion {    uialertview *alert = [[ uialertview alloc] initwithtitle:title message:message delegate:self  cancelbuttontitle:nil otherbuttontitles:button1title, button2title, nil];     objc_setassociatedobjECT (alert, eocmyalertviewkey, completion, objc_association_copy);     [alert  show];} -  (void) Showchoicealert: (nsstring *) Message button1title: (nsstring *) title1  Button2title: (nsstring *) Title2 button3title: (nsstring *) Title3 completion: ( Choicecompletionblock) Completion{    uialertview *alert = [[uialertview  alloc] initwithtitle:tiptitle message:message delegate:self cancelbuttontitle:nil  otherbuttontitles:title1, title2, title3, nil];    objc_ Setassociatedobject (alert, eocmyalertviewkey, completion, objc_association_copy);     [alert show];}
#pragma mark-uialertviewdelegate-(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (Nsinteger)    buttonindex{Choicecompletionblock block = Objc_getassociatedobject (Alertview, Eocmyalertviewkey);    if (block) {block (Buttonindex); }}

Using the runtime association can be a good solution to the problem, do not use arrays or anything!

iOS combat-using Block's Uialertview

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.