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