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调用通知
随机推荐
- cxf设置代理访问webservice接口
由于业务上的需要,需要访问第三方提供的webservice接口,但由于公司做了对外访问的限制,不设置代理是不能外网的,如果使用http设置代理访问外网还是比较容易的,但使用cxf有点不知道从哪里入手. ...
- css3 input边框蓝光特效
1.style样式 <style type="text/css"> .demok3_input { transition: all 0.30s ease-in-out; ...
- HTML-如何让自己的网页标题处可以显示网站的logo?
<link rel="Bookmark" href="/forum_images/ffico.png" /> <link rel=" ...
- Python Day2
一.列表 列表是我们最以后最常用的数据类型之一,通过列表可以对数据实现最方便的存储.修改等操作 定义列表 新建一个列表 name = ['ShaoLin','Tom','Kimi','Rain','A ...
- yii2 session的使用方法
yii2打开session use yii\web\Session; $session = Yii::$app->session; // check if a session is alread ...
- thinkphp 3.2与phpexcel
thinkphp版本:3.2 1.在http://phpexcel.codeplex.com/下载最新PHPExcel 2.把Classes目录下的文件(PHPExcel.php和PHPExcel文件 ...
- centos7安装图片界面
yum groupinstall "GNOME Desktop" "Graphical Administration Tools"
- IIS ARR 负载均衡
阅读:http://www.cnblogs.com/jesse2013/p/dlws-loadbalancer2.html 自定义端口:http://www.th7.cn/Program/net/20 ...
- Express知识整理
开发实例 Express开发实例(1) —— Hello,world! Express开发实例(2) —— Jade模板引擎
- Silicon C8051F340之GPIO口配置与使用
一.背景: 很久前用过C8051,现在有相关需求需要重新使用C8051,然后发现一年前开发的相关经验都忘得 基本上差不多了.连最基本的GPIO口配置还得重新来看手册,所以有此文,做个记录,以备下次快速 ...