IOS tries to replace delegate implementation with block closures

Source: Internet
Author: User

This is usually the case. Create alert and add an agent
// Create a Uialertview and show it Delegate : Self cancelbuttontitle:str otherbuttontitles:nil]; [Alertview show]; -(void) Alertview: (Uialertview *) Alertview Clickedbuttonatindex: (nsinteger) buttonindex{    nslogd (  @ "%ld", (long) buttonindex);}
if it looks like the bottom of the writing is much more [[[Uialertview alloc] Initwithtitle:@"Delete this Item?"message:@"Is you sure want to delete this really important thing?"Cancelbuttonitem:[ributtonitem Itemwithlabel:@"Yes"action:^{                                      //Handle "Cancel"}] Otherbuttonitems:[ributtonitem Itemwithlabel:@"Delete"action:^{                                       //Handle "Delete"}], nil] show];


Got lazy cancer just go to this link download directly or look down. Highly customizable https://github.com/jivadevoe/UIAlertView-Blocks

is to implement the method when you want to implement the Block method xxx.action (), of course, to pre-determine if there is this method if (xxx.action) //This is a non-parametric closure if you want to write a parameter closure see The wording of closures

////HFLittleHelperButton.h//Dailylife////Created by HF on 15/12/5.////#import"HFBorderLineButton.h"@interface hflittlehelperbutton:hfborderlinebutton@property (copy, nonatomic)void(^action) ();+ (ID) buttonwithnormaltitle: (NSString *) title action: (void(^) (void) ) Action;@end
The key method has been made to highlight the emphasis that should be seen on how to invoke this. The action method can also get the BTN object of this implementation where needed and then trigger. Action () can be
////hflittlehelperbutton.m//Dailylife////Created by HF on 15/12/5.////#import"HFLittleHelperButton.h"@implementation Hflittlehelperbutton+ (ID) buttonwithnormaltitle: (NSString *) title action: (void(^) (void)) action{Hflittlehelperbutton*BTN =[Hflittlehelperbutton Buttonwithtype:uibuttontypecustom]; [Btn Settitle:title Forstate:uicontrolstatenormal]; [Btn Settitlecolor:color_theme_green Forstate:uicontrolstatenormal]; [btn Settitlecolor:[uicolor Whitecolor] forstate:uicontrolstatehighlighted]; [btn setbackgroundimage:[huofarutils Createimagewithcolor:color_theme_green] Forstate:uicontrolstatehig Hlighted]; Btn.backgroundcustomcolor=Color_theme_green; Btn.layer.cornerRadius=2; Btn.layer.masksToBounds=YES; Btn.layer.borderColor=Color_theme_green. Cgcolor; Btn.layer.borderWidth=1; [btn Setaction:action];

[Btn addtarget:self Action: @selector (btnblockaction:) forcontrolevents:uicontroleventtouchupinside];

    Return

-(void) Btnblockaction: (Hflittlehelperbutton *) btn{

if (btn.action)

Btn.action ();

}

@end

Actual combat:
Hflittlehelperalertview *alert = [[Hflittlehelperalertview alloc]initwithblockexpression: Helperalertviewexpressiontypeplease Title:@"Title name"Message@" Good study day day up Good study day day up Good study day day up Good study day day up good study day up"Withview:nil Cancelbutton:[hflittlehelperbutton Buttonwithcanceltitle:@"Cancel"action:^{NSLog (@"XXXXXX"); }] Otherbuttons:[Hflittlehelperbutton buttonwithnormaltitle:  @ "111111" action:^{NSLog (@ "111111"  );}],[Hflittlehelperbutton buttonwithnormaltitle:  @ "222222" action:^{NSLog (@ "222222"  );}],[Hflittlehelperbutton buttonwithnormaltitle:  @ "333333" action:^{NSLog (@ "333333"  );}], nil]; [Alert Showhelperalertviewview];




IOS tries to replace delegate implementation with block closures

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.