The main thread and child threads often need to pass data. Between different classes, there is a need between different controllers.
And it is often necessary to listen for an action to complete. And then to do the corresponding events.
(The agent is a one-to-one relationship).
First, the agent value
A proxy is a design pattern.
Many in iOS. The ability to pass values by defining proxies themselves.
See this article of mine http://blog.csdn.net/xn4545945/article/details/31036523
In iOS. There are 3 steps to be achieved between the entrusted party and the entrusted party. (See Code gaze)
The following code: the UI needs to be updated after the simulation download is complete. Use proxies to pass values.
XNUpload.h
#import <FOUNDATION/FOUNDATION.H>//1. Defining the Proxy protocol @protocol xnuploaddelegate <nsobject>-(void) Downloadfinshed: (NSString *) param; @end @interface xnupload:nsobject//2. Define Proxy Properties @property (nonatomic, assign) ID < Xnuploaddelegate> delegate;-(void) download; @end
Xnupload.m
#import "XNUpload.h" @implementation xnupload/** * after the download is complete, you need to update the UI */-(void) Download {NSLog (@ "Downloading ...."); Sleep (3.0 ); NSLog (@ "Download complete!"); /3. To update the UI in the main thread, you need to pass the number of references. Notifies the main thread that the download is complete. (Invoke Proxy method) [Self.delegate downloadfinshed:@ "Download complete!"];} @end
Xnviewcontroller.m
#import "XNViewController.h" #import "XNUpload.h" @interface Xnviewcontroller () <XNUploadDelegate>//1. Abide by the agent agreement @end@implementation xnviewcontroller-(void) viewdidload {[Super viewdidload]; Xnupload *upload = [[Xnupload alloc] init]; [UPLOAD download]; Downloaded the//2. Set proxy upload.delegate = self;} 3. Implement the Proxy method. This means that the download is complete.-(void) downloadfinshed: (NSString *) param {NSLog (@ "%@", param);} @end
second, block code transfer value
Block code is easier to pass and the code is more compact. More convenient. Block code is more common in multithreading.
Use note:1.When not setBlockandIncomingNilwhen,will report the wild handsexc Badaccess. (Solve:Infer,if (xx) {..})2.Blockinrun should go back to the main threadto updateUI,into the insideDispatchA bit.3.Apple suggests that all theBlock Code DefinitionAllDon'tplaced inheader Filein.(Solve:directly in the use of block codeto an anonymous.justOK)*Advantages:can beAvoidCustom block code name with Apple built -inConflicting names.
the code is as follows:XNUpload.h
#import <Foundation/Foundation.h> @interface xnupload:nsobject-(void) Download: (Void (^) (nsstring *param)) completion; @end
Xnupload.m
#import "XNUpload.h" @implementation xnupload/** * The UI needs to be updated after the download is complete. Use block code. */-(void) Download: (Void (^) (NSString * param)) Completion {//1. Define the Block code NSLog (@ "Downloading ...") in the number of parameters; Sleep (3.0); NSLog (@ "Download complete!"); /update UI in the main thread, you need to pass the number of references. Notifies the main thread that the download is complete. (use block Code) if (completion) {//This allows the caller not to care about threading issues Dispatch_async (Dispatch_get_main_queue (), ^{ completion (@ "Download complete!"); 2. Run block Code});}} @end
Xnviewcontroller.m
#import "XNViewController.h" #import "XNUpload.h" @interface Xnviewcontroller () @end @implementation xnviewcontroller- (void) Viewdidload {[Super viewdidload]; Xnupload *upload = [[Xnupload alloc] init];//Direct call method can be [upload download: ^ (nsstring *param) { NSLog (@ "%@", [Nsthread C Urrentthread]); NSLog (@ "%@", param);}];} @end
The block code is more than the agent. It is very easy to use.
Reprint Please specify source:http://blog.csdn.net/xn4545945
"IOS" proxy pass-through value and block code transfer value