iOS NSOperation 封装 通知实现界面更新
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MYOperation : NSOperation
@end
#import "MYOperation.h"
@implementation MYOperation
-(void)main
{
//不管是ARC还是MRC一定要用autorelease来释放c语言对象
@autoreleasepool {
//NSString *image=[self stringEith];
//发布通知
//[[NSNotificationCenter defaultCenter] postNotificationName:@"MYOperation" object:image];
[self stringEith];
[self downLoadImage:@"URL"];
}
}
//模拟下载 跳到主进程模拟发送通知,通知控制器更新UI
-(NSString*)stringEith
{
NSString*image= @"大坏蛋!!!!";
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MYOperation" object:image];
}];
return image;
}
//下载
-(UIImage *)downLoadImage:(NSString*)str
{
NSURL *url=[NSURL URLWithString:str];
NSData *data=[NSData dataWithContentsOfURL:url];
UIImage*image=[UIImage imageWithData:data];
[[NSOperationQueue mainQueue]addOperationWithBlock:^{
[[NSNotificationCenter defaultCenter] postNotificationName:@"MYOperation" object:image];
}];
//return image;
return image;
}
@end
//控制器代码
#import "ViewController.h"
#import "MYOperation.h"
@interface ViewController ()
@property(nonatomic,strong)NSOperationQueue*queue;
@property(nonatomic,strong)NSString*str;
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
//发送通知的是在子进程,这里接收通知执行方法也是在子进程中执行,,发送通知是在主进程,这里接收通知执行方法也是在主进程中执行,一般情况下更新UI是在主进程中进行,所以这里我们应该默认让接收事件方法在主进程中执行\
需要在拓展类中进行操作
//注册通知事件
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateImage:) name:@"MYOperation" object:nil];
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
MYOperation *operation=[[MYOperation alloc]init];
//监听
[self.queue addOperation:operation];
}
//
-(void)updateImage:(NSNotification*)notification
{
self.str=notification.object;
NSLog(@"%@ ,%@",self.str,[NSThread currentThread]);
}
//注册的通知事件一定要移除
-(void)dealloc
{
[[NSNotificationCenter defaultCenter]removeObserver:self];
}
-(NSOperationQueue*)queue
{
if (!_queue) {
_queue=[[NSOperationQueue alloc]init];
//设置最大线程数
[_queue setMaxConcurrentOperationCount:6];
}
return _queue;
}
@end
iOS NSOperation 封装 通知实现界面更新的更多相关文章
- iOS NSOperation
iOS NSOperation 一.简介 除了,NSThread和GCD实现多线程,配合使用NSOperation和NSOperationQueue也能实现多线程编程 NSOperation和NSOp ...
- iOS开发系列文章(持续更新……)
iOS开发系列的文章,内容循序渐进,包含C语言.ObjC.iOS开发以及日后要写的游戏开发和Swift编程几部分内容.文章会持续更新,希望大家多多关注,如果文章对你有帮助请点赞支持,多谢! 为了方便大 ...
- iOS push过去的时候界面不能完全退出
iOS push过去的时候界面不能完全退出 解决方法:设置self.view.backgroundcolor 1. initWithFrame方法是什么? initWithFrame方法用来初始化并 ...
- windows程序消息机制(Winform界面更新有关)
windows程序消息机制(Winform界面更新有关) 转自:http://www.cnblogs.com/blosaa/archive/2013/05/31/3109586.html 1. Win ...
- iOS Notification – 远程通知
本文讲解iOS的远程通知的基本使用,主要包括远程通知的类型,处理远程通知的场景,以及远程通知相关证书的配置等等. 一.APNs简介 APNs是苹果公司提供的远程通知的服务器,当App处于后台或者没有运 ...
- iOS学习——(转)iOS中关于通知的使用
在移动端开打过程中,经常会用到通知和推送,例如有短信来了需要通知提示,手机横屏了需要通知提示,插上耳机了需要通知提示等等,我们可以根据这些通知采取对应的动作.iOS系统自身定义了很对通知,但是在开发过 ...
- Sprint 5 summary: UI 界面更新,Azure端部署和用户反馈分析 12/28/2015
本次sprint主要完成的任务有对手机APP的UI界面的更新,同时对Azure客户端的部署进行了相应的学习和有关的程序设计.同时对于ALPHA release的用户反馈做出相应的分析以确定接下来工作的 ...
- iOS 15 Beta升级卡死在更新进程,无法启动怎么办?
2021苹果全球开发者大会结束后,大批果粉迫不及待的尝试升级iOS 15测试版本,想第一时间体验新功能. 但是许多用户反馈升级一直卡死在"准备更新"."验证更新" ...
- 李洪强iOS开发之通知的使用
李洪强iOS开发之通知的使用 01 - 在A中发送通知 02 - 在B中监听通知 03 - 在B中通知出发的方法 04 - 在B控制器viewDidLoad调用通知
随机推荐
- a版本冲刺第十天
队名:Aruba 队员: 黄辉昌 李陈辉 林炳锋 鄢继仁 张秀锋 章 鼎 408: 十天体会:完成冲刺很开心,大家一起为同一件事情努力的感觉还是很不错的,众人拾柴火焰高,而且冲刺的时候会有一种压 ...
- 配置linux----------------ip
在终端中输入 vi /etc/sysconfig/network-scripts/ifcfg-eth0 =================================== DEVICE=" ...
- centos 7 安装 python-dev包提示No package python-dev available
centos安装 python-dev包提示No package python-dev available: 出现此问题的原因是python-dev的包在centos的yum中不叫python-dev ...
- Makefile 规则的使用
1.Makefile格式 //最终目标 all: led.o //依赖 arm-linux-ld -Tled.lds -o led.elf led.o //命令 arm-linux-objcopy - ...
- SQL case
case when Value='1' then 'True' else 'False' end as 'Result'
- AutofacContainer
/// <summary> /// Autofac容器 /// </summary> public class AutofacContainer { public static ...
- Android Studio的配置问题——Intel HAXM is required to run this AVD,VT-x is disabled in BIOS;
Intel HAXM is required to run this AVD,VT-x is disabled in BIOS;Enable VT-x in your BIOS security se ...
- 扼杀 304,Cache-Control: immutable
随着近些年社交网站的流行,越来越多的人学会了“刷”网页 ── 刷微博,刷朋友圈,刷新闻,刷秒杀页.这里的“刷”,就是刷新的意思,在浏览器里,你可以通过点击刷新按钮,或者用快捷键,或者移动端的下拉操作来 ...
- IK分词器 整合solr4.7 含同义词、切分词、停止词
转载请注明出处! IK分词器如果配置成 <fieldType name="text_ik" class="solr.TextField"> < ...
- 一段freemarker高级分页效果的代码
<a onclick="page(1)">首页</a> [#if currpage != 1] [#assign last=currpage - 1] &l ...