Standard Implementation of ios callback functions: protocol + delegate

Source: Internet
Author: User

I. project structure

  

Ii. Main Code

1. CoreManage. h

# Import <Foundation/Foundation. h> @ protocol SampleProtocol; // declare the attributes and methods of the core class @ interface CoreManage: NSObject @ property (nonatomic, assign) id <SampleProtocol> delegate;-(void) doMainWork; @ end // declare the interface function @ protocol SampleProtocol <NSObject> @ required-(void) innerTaskCallBack :( int) innerTaskSerialNo; @ optional-(void) resultDisplayCallBack :( NSString *) backInfo; @ end

2. CoreManage. m

# Import "CoreManage. h "@ implementation CoreManage @ synthesize delegate = _ delegate;-(id) init {self = [super init]; if (self) {// initialization code} return self ;} -(void) doMainWork {NSLog (@ "core class completes process 0"); NSLog (@ "core class completes process 1"); [self. delegate innerTaskCallBack: 2]; // process 2 NSLog is completed through the external class callback function (@ "core class completes process 3"); [self. delegate resultDisplayCallBack: @ "success"]; // display the task completion result through the external class callback function} @ end

3. OuterClass. h

#import <Foundation/Foundation.h>#import "CoreManage.h"@interface OuterClass : NSObject<SampleProtocol>- (void)run;@end

4. OuterClass. m

# Import "OuterClass. h "@ implementation OuterClass-(void) run {CoreManage * coreManage = [[CoreManage alloc] init]; coreManage. delegate = self; [coreManage doMainWork];} // callback function 1-(void) innerTaskCallBack :( int) innerTaskSerialNo {NSLog (@ "external class callback function, process % d ", innerTaskSerialNo);} // callback function 2-(void) resultDisplayCallBack :( NSString *) backInfo {NSLog (@" external class callback function, output the completion result: % @ ", backInfo);} @ end

5. AppDelegate. m

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];            OuterClass *outerClass=[[OuterClass alloc]init];    [outerClass run];        self.window.backgroundColor = [UIColor whiteColor];    [self.window makeKeyAndVisible];    return YES;}

Iii. Running results

  

 

Refer:

Network Communication and UI combined callback:

Http://blog.csdn.net/z251257144/article/details/7175516

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.