[New Learn] explains OBJECTIVE-C's block knowledge-practice

Source: Internet
Author: User

1. Introduction

The knowledge of block is described in the previous article [New learn] explaining the block knowledge of objective-c. In this chapter we will take a practical example to briefly describe how block replaces proxies.

2. The original through the Proxy realization Way review

Using Xib to create a view in [how], we show how the Footview from the Xib table can be used to notify the controller by proxy to load more data.

Corresponding Code: Https://github.com/xufeng79x/tableView_groupbuy_test

1. In the above project we have declared an agreement in the XFLoadMoreView.h header file

@protocol Xfloadmoreviewdelegate <NSObject>/* * *when the button is clicked, notify the  agent  Implementation Object **/@optional -(void) LOADMOREVIEWDIDCLICKEDTOLOADBTN: (Xfloadmoreview *) Loadmoreview; @end

2. The proxy attribute is declared in the custom Xfloadmoreview:

ID delegate;

3. The protocol is implemented in the controller, and the Proxy property value is set to the current controller when the Xfloadmoreview instance is created to make the notification call.

@interfaceXfgoodsshowsviewcontroller () <uitableviewdatasource, xfloadmoreviewdelegate>......  //This method is called after the load button is pressed to notify this object-(void) LOADMOREVIEWDIDCLICKEDTOLOADBTN: (Xfloadmoreview *) loadmoreview{//here we simulate adding a group buy message, inserting the best line into the listXfgoodmodel *model =[[Xfgoodmodel alloc] init]; Model.image=@"image.jpg"; Model.name=@"New Food"; Model.price=123; Model.soldnum=321; //inserting secondary information into a table data source[Self.goodslist Addobject:model]; //this data needs to be plugged into the last row of the tableNsindexpath *indexpath = [Nsindexpath indexPathForItem:self.goodsList.count-1Insection:0];        [Self.goodstableview Insertrowsatindexpaths:@[indexpath] withrowanimation:uitableviewrowanimationtop]; //when the load is complete, roll the table to the newly added row, which is the last bar[Self.goodstableview Scrolltorowatindexpath:indexpath Atscrollposition:uitableviewscrollpositionbottom Animated:yes];} .... 

3. Use the block feature to override the proxy feature mechanism

We can do it by proxy without using the protocol, we can deal with it by Block:

The specific methods are:

1. Declare the Block property in the XFLoadMoreView.h file to receive a callback block from the target

// Defining notification Callbacks void (^notifyloadblock) (Xfloadmoreview *);

2. Add the block parameter to the XFLOADMOREVIEW.M class initialization method

//initialization method of notification callback using block+ (Instancetype) Loadmoreviewwithnotifyblock: (void(^) (Xfloadmoreview *)) notifyloadblock{//load view directly from XibXfloadmoreview *loadview = [[[NSBundle Mainbundle] loadnibnamed:@"Xfloadbutton"Owner:nil Options:nil] lastobject]; //beautify button to round the four corners of a buttonLoadView.loadMoreBtnView.layer.cornerRadius =5; LoadView.loadMoreBtnView.layer.masksToBounds=YES; //set Load more callback blockLoadview.notifyloadblock =Notifyloadblock; returnLoadview;}

3. When initializing the Xfloadmoreview at the controller, the specific block implementation is passed in:

    // load the TableView footview    Xfloadmoreview *loadmoreview = [Xfloadmoreview loadmoreviewwithnotifyblock:^ (Xfloadmoreview * view) {        [self Loadmoreviewdidclickedtoloadbtn:view];    }];

The methods in the original protocol method are reused here.

4. When a Load event occurs in a custom Xfloadmoreview, notify the controller to load the data:

/** * Trigger after clicking Load More button*/-(ibaction) Loadmoreclick {//hide Botton to render after loading a styleSelf.loadMoreBtnView.hidden =YES; Self.loadMoreIngView.hidden=NO; //Use this method to achieve a deferred execution effectDispatch_after (Dispatch_time (Dispatch_time_now,2.0* nsec_per_sec), Dispatch_get_main_queue (), ^{Self.loadMoreBtnView.hidden=NO; Self.loadMoreIngView.hidden=YES; //send information to the agent, because it is an optional method set in the proxy definition, you need to check whether this method is implemented before sending the information//if ([Self.delegate respondstoselector: @selector (loadmoreviewdidclickedtoloadbtn:)])//        {//[Self.delegate loadmoreviewdidclickedtoloadbtn:self];//        }                //using callbacks to load notifications        if(Nil! =self.notifyloadblock) {self.notifyloadblock (self); }    });

4. Summary

In this article, we have implemented a block approach to a function that was originally implemented by proxy.

So when should the agent adopt block?

We can refer to the following article: Http://stackoverflow.com/questions/26791548/objc-memory-usage-of-delegate-vs-block

This chapter code: Https://github.com/xufeng79x/BlockDemo

[New Learn] explains OBJECTIVE-C's block knowledge-practice

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.