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,它是一种轻量级的 ...
随机推荐
- sipex3232上电发送输入端拉低问题
之前没怎么注意,在使用下载盒通过isp下载stm32时,由于是通过stm32板子给下载盒供电,所以是我的stm32板子和下载盒同时上电.这样总是不能够进入下载模式,后来对下载盒进行仿真,发现在下载发送 ...
- Jenkins——构建、集成中的问题
准备 Jekins 安装插件 MsBuild Plugin Team Foundation Server Plug-in 配置环境变量 我的电脑中设置环境变量,Path中添加msbuild的路 ...
- 项目游戏开发日记 No.0x000003
14软二杨近星(2014551622) 刚刚过去清明节, 意味着离交项目的时间, 还有三个星期, 有点着急了, 可是, 还是觉得无所适从... 项目进展: 刚刚过去的一周, 事非常多, 以至于, 进展 ...
- hibernate关联映射
首先,建立user表,news表 建立User,News类 package com.example.hibernate; import java.util.Set; public class User ...
- HTML 学习笔记 JQuery(DOM 操作)
一般来说,DOM操作分为三个方面,即:DOM Core(核心), HTML_DOM 和 CSS_DOM. 1.DOM Core DOM Core 并不专属于JavaScript,任何一种支持DOM的程 ...
- UploadHandleServlet
public class UploadHandleServlet extends HttpServlet { public void doGet(HttpServletRequest request, ...
- BZOJ 4008 【HNOI2015】 亚瑟王
题目链接:亚瑟王 这道题好神啊TAT--果然我的dp还是太弱了-- 一开始想了半天的直接dp求期望,结果最后WA的不知所云-- 最后去翻了题解,然后发现先算概率,再求期望--新姿势\(get\). 我 ...
- 解读ASP.NET 5 & MVC6系列(13):TagHelper
在新版的MVC6中,微软提供了强大的TagHelper功能,以便让我们摆脱如下的臃肿代码: @Html.LabelFor(model => model.FullName) @Html.EditF ...
- 理解ASP.NET MVC的DependencyResolver组件
一.前言 DependencyResolver是MVC中一个重要的组件,从名字可以看出,它负责依赖对象的解析,可以说它是MVC框架内部使用的一个IOC容器.MVC内部很多对象的创建都是通过它完成的,或 ...
- Java实现多种方式的http数据抓取
前言: 时下互联网第一波的浪潮已消逝,随着而来的基于万千数据的物联网时代,因而数据成为企业的重要战略资源之一.基于数据抓取技术,本文介绍了java相关抓取工具,并附上demo源码供感兴趣的朋友测试! ...