iOS Development Block (ii) Implementation delegation

Source: Internet
Author: User

Delegates and blocks are two mechanisms of implementing callbacks on iOS. Block can basically replace the function of the delegate, and the implementation is relatively concise, it is recommended to use the block of the place do not use the delegation.

Achieve results

The first step, custom Customcell

  1  #import <  foundation  /foundation.h  >  Span style= "color: #008080;" > 2   3   @interface Customcell: UITableViewCell   4   5   @property (strong, nonatomic) Iboutlet UILabel *labname;    @property (copy,nonatomic) void (^buygoods) ( NSString *);    8  -( ibaction) buttonbuypressed: (ID) sender;   10  @end 
1 #import "CustomCell.h" 2 3 @implementation Customcell 4 5 6 7 -(Ibaction) buttonbuypressed: (ID) Sender {  8      9    nsstring *goodname=_ Labname.text; Ten     _buygoods (goodname);  One      A } @end

In the custom.h file there are

@property (copy, nonatomic) void(^buygoods) (nsstring *);

Declares a block variable

Callback inside the CUSTOM.M file

nsstring *goodname=_labname. text;

_buygoods (goodname);

See below the implementation of the class inside

1 #import "ViewController.h"2 #import "CustomCell.h"3 4 @interface Viewcontroller ()5 6 @end7 8 @implementation Viewcontroller9 Ten -(void) Viewdidload One { A [Super Viewdidload]; - additional setup after loading the view, typically from a nib. -      the _tabgoods.delegate=self; - _tabgoods.datasource=self; - } -  + -(void) didreceivememorywarning - { + [Super didreceivememorywarning]; A //Dispose of any resources, can be recreated. at } -  - -(UITableViewCell *) TableView: (UITableView *) TableView Cellforrowatindexpath: (Nsindexpath *) Indexpath - { - static NSString *cellidentifier = @ "Customcell"; - Customcell *cell = (customcell*) [TableView dequeuereusablecellwithidentifier:cellidentifier]; in if (cell = = nil) { - Nsarray *nib = [[NSBundle mainbundle] loadnibnamed:@ "Customcell" owner:self Options:nil]; to cell = [nib objectatindex:0]; +     } - Cell.labName.text = [nsstring stringwithformat:@ "Product name%d", Indexpath.row]; the __block nsstring *tipmsg=cell.labname.text; * cell. buygoods=^ (NSString *str) { $ uialertview *alertview=[[uialertview alloc]initwithtitle:@ "prompt content" message:tipmsg Delegate:nil cancelButtonTitle : @ "Cancel" otherbuttontitles:nil, nil];Panax Notoginseng [Alertview show]; -     }; the      + return cell; A } the  + -(Nsinteger) Numberofsectionsintableview: (UITableView *) tableview{ - return 1; $ } $  - -(Nsinteger) TableView: (UITableView *) TableView numberofrowsinsection: (nsinteger) section{ - return ten; the } - Wuyi@end

Inside the viewcontroller.m file

__block nsstring *tipmsg=cell. Labname. text;

Cell. buygoods=^ (nsstring *str) {

Uialertview *alertview=[[uialertview alloc]initwithtitle:@ " prompt content " message:tipmsg delegate:nil cancelbuttontitle:@ " cancel " otherbuttontitles:Nil , Nil];

[Alertview show];

};

Implement block inside block code, __block nsstring *tipmsg=cell. Labname. this line guarantees that the variable can be used in block code blocks ....

Run:

iOS Development Block (ii) Implementation delegation

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.