[Android and IOS development comparison series] callback mechanism, androidios

Source: Internet
Author: User

[Android and IOS development comparison series] callback mechanism, androidios
Callback mechanism of [Android and IOS development comparison series]

The implementation principle of callback for Android and IOS is basically the same, but the specific name is different.

This article will summarize the Delegate and block of IOS, Android interfaces and Handler.

IOS Protocol

DelegateDelegateIs a protocol.@protocolStatement. The role of the delegate class is to transmit values, and to upload events.

For example:

To select an album image, follow the figure below:
Class C is a delegate class and defines a value passing method.
Class A displays an image and click the button to go to class B to select an image.
After selecting an image in Class B, the methodC: para method is implemented.
Return A and execute the callback method:
MethodC: para {
// Get imageView Url from para
}

Android Interface

CallbackCallbackUsing InterfacesinterfaceClass.

For example:

You can also follow the IOS example,
Let's look at the following: to call click events
Class C is an interface class, View. OnClickListener,
A is an Activity, B is A Button,
Initialize A, Button btn,
Btn. setOnClickListener (this ),
View. OnClickListener (){
}
B calls View. onClickListener IN THE onTouch event.
Well, I'm familiar with it.

IOS block

IOS hasBlockThe mechanism is not available for Android and is super easy to use.

The following three types of code are described:

1. First:

typedef void (^ArrayResultBlock)(NSArray *objects, NSError *error);[self queryByStory:self.story callback:^(NSArray *objects, NSError *error) {}];-(void)queryByStory:(Story *)story callback:(ArrayResultBlock)block{    [_dbQueue inDatabase:^(FMDatabase *db) {        if (block) {            block(result, nil);        }    }];}

2. Second:

    VC *vc = [VC new];    vc.typeBlock = ^(NSString * result){        //do anything    };    @property (nonatomic, copy) void(^typeBlock)(NSString *);    - (void)method{        if(_typeBlock){            _typeBlock(@"end data restored");        }

3. Third: block nesting

typedef void (^CommonResultBlock)(BOOL successed);[self saveStory:^(BOOL successed) {        [self saveMore:^(BOOL successed) {        }];    }];-(void) saveStory:(CommonResultBlock)block{    if(block){        block(YES);    }}-(void) saveMore:(CommonResultBlock)block{    if(block){        block(YES);    }}
Android Handler

If the block is only available in IOS, Android certainly does not accept it because Android has a Handler.

In fact, Handler and block play the same role, but they are not a concept in principle.

The following code uses Handler as an example:

private Handler handler = new Handler() {        @Override        public void handleMessage(Message msg) {            if (msg.what == UPDATE) {                tv.setText(String.valueOf(msg.obj));            }            super.handleMessage(msg);        }    };
New Thread () {@ Override public void run () {try {Message msg = new Message (); msg. what = UPDATE; msg. obj = "updated value:" + I; handler. sendMessage (msg) ;}} catch (InterruptedException e) {e. printStackTrace ();}}}. start ();

Of course, Handler can also be nested like block.

In addition:
If you have any opinions or questions, please discuss them and correct them.

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.