For object-c @ protocol, object-c @ protocol

Source: Internet
Author: User

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

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.