[IOS] Pass the value by proxy and block code, and pass the code by ios proxy
Data transmission is often required between the main thread and the subthread. Different classes and controllers are required.
In addition, you often need to listen to the completion of an action before proceeding to the corresponding event. (Proxy is a one-to-one relationship ).
I. Pass values by proxy
Proxy is a design mode. IOS uses a lot of resources. You can use a custom proxy to transmit values.
See my article http://blog.csdn.net/xn4545945/article/details/31036523
In iOS, the entrusting party and the entrusted party must complete three steps. (See code comments)
The following code updates the UI After simulating the download. Use a proxy to pass the value.
XNUpload. h
# Import <Foundation/Foundation. h> // 1. define the proxy protocol @ protocol XNUploadDelegate <NSObject>-(void) downloadFinshed :( NSString *) param; @ end @ interface XNUpload: NSObject // 2. define proxy attributes @ property (nonatomic, assign) id <XNUploadDelegate> delegate;-(void) download; @ end
XNUpload. m
# Import "XNUpload. h "@ implementation XNUpload/*** update the UI */-(void) download {NSLog (@" download .... "); sleep (3.0); NSLog (@" Download complete! "); // 3. Update the UI to the main thread. You need to pass parameters to notify the main thread that the download is complete. (call the proxy method) [self. delegate downloadFinshed: @" the download is complete! "];} @ End
XNViewController. m
# Import "XNViewController. h "# import" XNUpload. h "@ interface XNViewController () <XNUploadDelegate> // 1. follow the proxy Protocol @ end @ implementation XNViewController-(void) viewDidLoad {[super viewDidLoad]; XNUpload * upload = [[XNUpload alloc] init]; [upload download]; // downloaded // 2. set proxy upload. delegate = self;} // 3. proxy method. this method indicates that the download is complete. -(void) downloadFinshed :( NSString *) param {NSLog (@ "% @", param);} @ end
Ii. Pass the block code value
The block code is easier to pass values, and the code is more compact and convenient. Block Code is more common in multithreading.
Note: 1. when nil is passed in without block setting, the wild pointer excbadaccess is reported. (solution: Judge, if (xx ){..}) 2. the execution in the block should go back to the main thread to update the UI and dispatch it to it. 3. apple recommends that you do not include all block code definitions in header files. (solution: You can use block code to obtain an anonymous one.) * benefits: avoid the conflict between the block code name defined by yourself and the built-in name of apple.
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/*** update the UI after the simulated download is complete. use block code. */-(void) download :( void (^) (NSString * param) completion {// 1. define the block code NSLog In the parameter (@ "download .... "); sleep (3.0); NSLog (@" Download complete! "); // Update the UI to the main thread. parameters must be passed. the master thread is notified of download completion. (using Block Code) if (completion) {// so that the caller does not have to worry about thread issues dispatch_async (dispatch_get_main_queue (), ^ {completion (@ "download completed! "); // 2. Execute the 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]; // call the method directly to [upload download: ^ (NSString * param) {NSLog (@ "% @", [NSThread currentThread]); NSLog (@ "% @", param) ;}] ;}@ end
Compared with the proxy, the block code is much simpler and more convenient to use.
Reprinted please indicate the source: http://blog.csdn.net/xn4545945
Transfer the value of the IOS Protocol from the first page to the second page
What is used for a page? Is the code written by storyboard? Using storyboard to develop the data transfer between pages is the easiest .. If you use code to write a value, you must pass the value before pushing it.
When an iOS int is converted to an nsstring, The nsstring parameter is incorrectly transmitted.
The called method is written together, and the program reports an error, whether it is a compilation error or a running error. If you think this code has changed every time the str value is used, in the following sentence, the str is a constant string, so is it an error caused by the limitations of calling methods.