[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
DelegateDelegate
Is a protocol.@protocol
Statement. 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
CallbackCallback
Using Interfacesinterface
Class.
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.