"Android vs. iOS development comparison series" callback mechanism
The callbacks for Android and iOS are basically the same, except for the exact naming.
This article will summarize iOS delegate and block, Android interface and handler.
IOS protocol
Delegate
A delegate is a kind of agreement, by @protocol
Declaration. The function of the delegate class, one is the value of the transmission, the second is the event.
As an example:
To implement the selected album image, according to the figure:
Class C is a delegate class that defines a method of passing a value.
Class A shows a picture, click the button, enter the Class B to select a picture.
When you finish selecting a picture in Class B, the Methodc:para method is implemented.
Return a, execute the callback method:
methodc:para{
Get ImageView URL from Para
}
Android interface
Callbacks Callback
are implemented using interfaces, by implementing interface
classes.
As an example:
You can also follow the example of iOS,
That's another: to implement a call to click events
Class C is the interface class, View.onclicklistener,
A is a activity,b is a button,
Initialize A, Button btn,
Btn.setonclicklistener (This),
View.onclicklistener () {
}
and b is called View.onclicklistener in the Ontouch event.
Well, it's familiar.
IOS Block
iOS has a block mechanism that is Android No, super easy to use.
Here are three kinds of code to explain:
1. The first type:
typedefvoid (^ArrayResultBlock)(NSArrayNSError *error);[self queryByStory:self.story callback:^(NSArrayNSError *error) {}];-(void)queryByStory:(Story *)story callback:(ArrayResultBlock)block{ [_dbQueue inDatabase:^(FMDatabase *db) { if (block) { nil); } }];}
2. The second type:
new]; result){ //do anything }; @propertycopy) void(^typeBlock)(NSString *); - (void)method{ if(_typeBlock){ _typeBlock(@"end data restored"); }
3. Third type: block nesting
typedefvoid (^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 iOS, Android must not be satisfied because Android has Handler.
In fact, the role of handler and block is the same, but the principle is not a concept at all.
The following code illustrates an example of a handler:
privatenew Handler() { @Override publicvoidhandleMessage(Message msg) { if (msg.what == UPDATE) { tv.setText(String.valueOf(msg.obj)); } super.handleMessage(msg); } };
new Thread() { @Override publicvoidrun() { try { new Message(); msg.what = UPDATE; "更新后的值:" + i; handler.sendMessage(msg); } catch (InterruptedException e) { e.printStackTrace(); } } }.start();
Of course, handler can also be nested like block.
Other than that:
If you have any opinions and questions, please feel free to discuss and correct them.
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
The "Android vs iOS development comparison series" callback mechanism