For object-c @ protocol, object-c @ protocol
Understanding from the java perspective
@ Protocol is equivalent to java interface definition, and the usage is the same
The following is an example of a test.
@protocol mytestClass <NSObject>- (void) callback:(int) num;@end@interface DiscoveryController : UIViewController<mytestClass>{ UIImageView *imageView; }-(UIImage *) getImageFromURL:(NSString *)fileURL;@end
Protocol is defined here and
DiscoveryController implements this interface
@interface MyCallBack : NSObject{ id<mytestClass> deleage;}@property(nonatomic,retain) id<mytestClass> deleage;- (void)setRun;@end
In MyCallBack, save
Call back the deleage when necessary.
//// MyCallBack. m // ACMobile // Created by mr. liang on 15/1/22. // Copyright (c) 2015 mr. liang. all rights reserved. // # import "MyCallBack. h "@ implementation MyCallBack @ synthesize deleage;-(void) runThread {NSLog (@" I am running "); [NSThread sleepForTimeInterval: 2]; [deleage callback: 1];} -(void) setRun {NSThread * callbackthread = [[NSThread alloc] initWithTarget: self selector: @ selector (runThread) object: nil]; [callbackthread start];} @ end
Asynchronous callback through threads makes the program more intuitive.
MyCallBack *vv = [[MyCallBack alloc] init]; vv.deleage = self; [vv setRun];
You need to use the protocol to implement this protocol and transmit the pointer;
One thing about the Internet: www.yidin.net