iOS-Block总结 && 全面解析逆向传值
Heros *hero = [HerosherosWithDict:dict];
Heros *heros = [HerosherosWithDict:dict];
[ArrM addObject:heros];
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- @class YSCNSOperationOP;
- typedef void(^setUpUIImage)(YSCNSOperationOP *);
- @interface YSCNSOperationOP : NSOperation
- @property (nonatomic, copy) NSString *urlString;
- @property (nonatomic, strong) UIImage *image;
- @property (nonatomic, copy) setUpUIImage myBlock;
- - (void)setUpUIImage:(setUpUIImage )block;
- @end
在适当的时候执行:
- #import "YSCNSOperationOP.h"
- @implementation YSCNSOperationOP
- - (void)main {
- @autoreleasepool {
- UIImage *image = [self downLoadImage:self.urlString];
- self.image = image;
- dispatch_async(dispatch_get_main_queue(), ^{
- self.myBlock(self);
- });
- }
- }
- - (UIImage *)downLoadImage:(NSString *)urlString{
- NSURL *url = [NSURL URLWithString:urlString];
- NSData *data = [NSData dataWithContentsOfURL:url];
- UIImage *image = [UIImage imageWithData:data];
- return image;
- }
- - (void)setUpUIImage:(setUpUIImage )block {
- if (block) {
- self.myBlock = block;
- }
- }
- @end
- #import "ViewController.h"
- #import "YSCNSOperationOP.h"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *iamgeView;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- YSCNSOperationOP *yscOp = [[YSCNSOperationOP alloc] init];
- yscOp.urlString = @"http://h.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8094ace24c9134954082376ee.jpg";
- [yscOp setUpUIImage:^(YSCNSOperationOP *op) {
- self.iamgeView.image = op.image ;
- }];
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [queue addOperation:yscOp];
- }
- @end
- #import <Foundation/Foundation.h>
- #import <UIKit/UIKit.h>
- @class YSCNSOperation;
- @protocol YSCNSOperationDelegate <NSObject>
- - (void)yscNSOperation:(YSCNSOperation *)operation withImage:(UIImage *)image;
- @end
- @interface YSCNSOperation : NSOperation
- @property (nonatomic, copy) NSString *urlString;
- @property (nonatomic, strong) UIImage *image;
- @property (nonatomic, weak) id<YSCNSOperationDelegate> delegate;
- @end
在需要的时候通知代理:
- #import "YSCNSOperation.h"
- @implementation YSCNSOperation
- - (void)main {
- @autoreleasepool {
- UIImage *image = [self downLoadImage:self.urlString];
- self.image = image;
- dispatch_async(dispatch_get_main_queue(), ^{
- if ([self.delegate respondsToSelector:@selector(yscNSOperation:withImage:)]) {
- [self.delegate yscNSOperation:self withImage:image];
- }
- });
- }
- }
- - (UIImage *)downLoadImage:(NSString *)urlString{
- NSURL *url = [NSURL URLWithString:urlString];
- NSData *data = [NSData dataWithContentsOfURL:url];
- UIImage *image = [UIImage imageWithData:data];
- return image;
- }
- @end
代理方:
- #import "ViewController.h"
- #import "YSCNSOperation.h"
- @interface ViewController () <YSCNSOperationDelegate>
- @property (weak, nonatomic) IBOutlet UIImageView *iamgeView;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- YSCNSOperation *yscOp = [[YSCNSOperation alloc] init];
- yscOp.delegate = self;
- yscOp.urlString = @"http://h.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8094ace24c9134954082376ee.jpg";
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [queue addOperation:yscOp];
- });
- }
- - (void)yscNSOperation:(YSCNSOperation *)operation withImage:(UIImage *)image {
- self.iamgeView.image = operation.image;
- }
- @end
- #import "YSCNSOperation.h"
- @implementation YSCNSOperation
- - (void)main {
- @autoreleasepool {
- UIImage *image = [self downLoadImage:self.urlString];
- self.image = image;
- dispatch_async(dispatch_get_main_queue(), ^{
- [[NSNotificationCenter defaultCenter] postNotificationName:@"setUpUI" object:self];
- });
- }
- }
- - (UIImage *)downLoadImage:(NSString *)urlString{
- NSURL *url = [NSURL URLWithString:urlString];
- NSData *data = [NSData dataWithContentsOfURL:url];
- UIImage *image = [UIImage imageWithData:data];
- return image;
- }
- @end
- #import "ViewController.h"
- #import "YSCNSOperation.h"
- @interface ViewController ()
- @property (weak, nonatomic) IBOutlet UIImageView *iamgeView;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(lookNotifi:) name:@"setUpUI" object: nil nil];
- }
- - (void)lookNotifi:(NSNotification *)notifi{
- YSCNSOperation *op= (YSCNSOperation *)notifi.object;
- self.iamgeView.image = op.image;
- //self.iamgeView.image = (UIImage *)notifi.object;
- }
- - (void)dealloc {
- [[NSNotificationCenter defaultCenter] removeObserver:self];
- }
- - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
- YSCNSOperation *yscOp = [[YSCNSOperation alloc] init];
- yscOp.urlString = @"http://h.hiphotos.baidu.com/image/pic/item/9825bc315c6034a8094ace24c9134954082376ee.jpg";
- NSOperationQueue *queue = [[NSOperationQueue alloc] init];
- [queue addOperation:yscOp];
- }
- @end
iOS-Block总结 && 全面解析逆向传值的更多相关文章
- iOS 代理与block 逆向传值 学习
一般在项目中出现逆向传值的时候就需要用到代理.block 或者通知中心了.由于公司的项目底层封装的很好,所以项目做了三四个月就算碰到需要逆传的情况也不用自己处理.但是最近遇到了一个特别的情况就需要自己 ...
- iOS Block界面反向传值
在上篇博客 <iOS Block简介> 中,侧重解析了 iOS Block的概念等,本文将侧重于它们在开发中的应用. Block是iOS4.0+ 和Mac OS X 10.6+ 引进的对C ...
- NSNotification,NSNotificationCenter的使用、iOS中五种对象间传值的方式
学习内容 NSNitification与NotificationCenter(通知与通知中心) 通知的使用 [[NSNotificationCenter defaultCenter]addObserv ...
- iOS block从零开始
iOS block从零开始 在iOS4.0之后,block横空出世,它本身封装了一段代码并将这段代码当做变量,通过block()的方式进行回调. block的结构 先来一段简单的代码看看: void ...
- iOS Crash文件的解析
iOS Crash文件的解析 开发程序的过程中不管我们已经如何小心,总是会在不经意间遇到程序闪退.脑补一下当你在一群人面前自信的拿着你的App做功能预演的时候,流畅的操作被无情地Crash打断.联想起 ...
- ios Block详细用法
ios Block详细用法 ios4.0系统已开始支持block,在编程过程中,blocks被Obj-C看成是对象,它封装了一段代码,这段代码可以在任何时候执行.Blocks可以作为函数参数或者函数的 ...
- [转载]iOS 10 UserNotifications 框架解析
活久见的重构 - iOS 10 UserNotifications 框架解析 TL;DR iOS 10 中以前杂乱的和通知相关的 API 都被统一了,现在开发者可以使用独立的 UserNotifica ...
- IOS的XML文件解析,利用了NSData和NSFileHandle
如果需要了解关于文档对象模型和XML的介绍,参看 http://www.cnblogs.com/xinchrome/p/4890723.html 读取XML 上代码: NSFileHandle *fi ...
- iOS学习——JSON数据解析(十一)
在之前的<iOS学习——xml数据解析(九)>介绍了xml数据解析,这一篇简单介绍一下Json数据解析.JSON 即 JavaScript Object Natation,它是一种轻量级的 ...
随机推荐
- linux基本知识0
linux的基本原则: 1.有目的单一的小程序组成,组合小程序完成复杂任务. 2.一切皆文件 3.尽量避免捕获用户接口 4.配置文件保存为纯文本格式 CLI接口: 命令提示符,prompt,bash ...
- Java 性能优化之 String 篇
原文:http://www.ibm.com/developerworks/cn/java/j-lo-optmizestring/ Java 性能优化之 String 篇 String 方法用于文本分析 ...
- NopCommerce 在Category 显示 Store List列表
实现效果如下: 1.在前台Web的Category Menu显示 Store; 2.点击 Store 显示 Store List列表: 3.点击 列表Store 的 Company Name 进入该S ...
- Unity中使用Attribute
Attribute是c#的语言特性 msdn说明如下: The Attribute class associates predefined system information or user-def ...
- 中大东校小米路由器mini实现inode上网,ipv6 wifi【中大】【东校】【inode】【ipv6】
还有不到4个月就要毕业了,前几天半夜没事捣鼓小米路由没想到竟然实现了wifi的ipv6. 正好又安利了同学一台小米路由mini,从刷机到inode到ipv6全搞了一遍. 这里将教程写出来,服务学弟妹. ...
- PAT 1047. 编程团体赛(20)
编程团体赛的规则为:每个参赛队由若干队员组成:所有队员独立比赛:参赛队的成绩为所有队员的成绩和:成绩最高的队获胜. 现给定所有队员的比赛成绩,请你编写程序找出冠军队. 输入格式: 输入第一行给出一个正 ...
- [LeetCode] Pascal's Triangle 杨辉三角
Given numRows, generate the first numRows of Pascal's triangle. For example, given numRows = 5,Retur ...
- eclipse环境搭建
百度经验http://jingyan.baidu.com/article/bea41d437a41b6b4c51be6c1.html 1.JAVA JDK 2.Andriod SDK eclipse里 ...
- logback logback.xml常用配置详解 <filter>
<filter>: 过滤器,执行一个过滤器会有返回个枚举值,即DENY,NEUTRAL,ACCEPT其中之一.返回DENY,日志将立即被抛弃不再经过其他过滤器:返回NEUTRAL,有序列表 ...
- git的基本介绍和使用
前言:从事iOS开发一年多以来,一直使用svn管理源代码.对svn的特点和弊端已经深有体会.前些天双十二前后,项目工期紧张到爆,起早贪黑的加班,可谓披星戴月,这还不止,回到家中还要疯狂的敲代码.那么问 ...