IOS回调机制——代理,通知中心以及Block
Xcode5.0正式版
代理模式
@protocol CallBackDelegate; @interface ViewController : UIViewController
@property (weak, nonatomic) id<CallBackDelegate> delegate;
@end @protocol CallBackDelegate <NSObject>
- (void)showArrayWithDelegate:(NSArray *)array;
@end
- (IBAction)delegateCallBack
{
NSDictionary *dict = @{@"array": @[@"Chelsea", @"MUFC", @"Real Madrid"]};
NSArray *array = dict[@"array"];
[self.delegate showArrayWithDelegate:array];
}
调用,显示
- (void)showArrayWithDelegate:(NSArray *)array
{
_outputLabel.text = array[2];
}
最重要也是最容易忽略的,就是一定要设置delegate的指向。
使用通知中心
- (IBAction)callBack
{
NSDictionary *dict = @{@"array": @[@"Chelsea", @"MUFC", @"Real Madrid"]};
NSArray *array = dict[@"array"];
[[NSNotificationCenter defaultCenter] postNotificationName:@"OutputArrayNotification" object:array];
}
注册和移出观察者
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(outputWithNote:) name:@"OutputArrayNotification" object:nil];
} - (void)viewDidDisappear:(BOOL)animated
{
[super viewDidDisappear:animated];
[[NSNotificationCenter defaultCenter] removeObserver:self name:@"OutputArrayNotification" object:nil];
}
显示
- (void)outputWithNote:(NSNotification *)aNotification
{
NSArray *receiveArray = [aNotification object];
_outputLabel.text = receiveArray[0];
}
Block
- (void)showArrayUsingBlock:(Arr_Block)block
{
NSDictionary *dict = @{@"array": @[@"Chelsea", @"MUFC", @"Real Madrid"]};
NSArray *array = dict[@"array"];
block(array);
}
调用方法,显示
- (IBAction)blockCallBack
{
[self showArrayUsingBlock:^(NSArray *array) {
_outputLabel.text = array[1];
}];
}
IOS回调机制——代理,通知中心以及Block的更多相关文章
- iOS开发之通知中心(NSNotificationCenter)
前言 面向对象的设计思想是把行为方法封装到每一个对象中,以用来增加代码的复用性.正是这种分散封装,增加了对象之间的相互关联,总是有很多的对象需要彼此了解以及相互操作! 一个简单示例说明这种交互产生的对 ...
- IOS Notification 通知中心
1. 通知中心概述 通知中心实际上是在程序内部提供了消息广播的一种机制.通知中心不能在进程间进行通信.实际上就是一个二传手,把接收到的消息,根据内部的一个消息转发表,来将消息转发给需要的对象. ...
- iOS通知中心升级 -可设置按优先级执行block
简单介绍下,这是需求驱动中发现iOS的NotificationCenter有很多功能无法实现,于是对其进行了一层包装.相当于手动管理观察者栈和监听者期望执行的事件,因此可以为其添加了很多新增的功能,将 ...
- iOS基础 - 通知中心(NSNotificationCenter)
通知中心(NSNotificationCenter) 每一个应用程序都有一个通知中心(NSNotificationCenter)实例,专门负责协助不同对象之间的消息通信 任何一个对象都可以向通知中心发 ...
- IOS NSNotification Center 通知中心的使用
通知中心,它是IOS程序内部的一种消息广播机制,通过它,可以实现无引用关系的对象之间的通信.通知中心他是基于观察者模式,它只能进行程序内部通信,不能跨应用程序进程通信.当通知中心接受到消息后会根据设置 ...
- iOS 通知中心扩展制作初步-b
涉及的 Session 有 Creating Extensions for iOS and OS X, Part 1 Creating Extensions for iOS and OS X, Par ...
- iOS监听模式系列之通知中心
补充--通知中心 对于很多初学者往往会把iOS中的本地通知.推送通知和iOS通知中心的概念弄混.其实二者之间并没有任何关系,事实上它们都不属于一个框架,前者属于UIKit框架,后者属于Foundati ...
- iOS中通知中心NSNotificationCenter应用总结
通知中心(NSNotificationCenter)实际是在程序内部提供了一种广播机制.把接收到的消息,根据内部的消息转发表,将消息转发给需要的对象.这句话其实已经很明显的告诉我们要如何使用通知了.第 ...
- iOS 通知中心 NSNotificationCenter
iOS开发中,每个app都有一个通知中心,通知中心可以发送和接收通知. 在使用通知中心 NSNotificationCenter之前,先了解一下通知 NSNotification. NSNotific ...
随机推荐
- 黑马程序员_Java面向对象_异常
6.异常 1.异常: 就是程序在运行时出现不正常的情况.问题也是现实生活中一个具体的事物,也可以通过Java的类进行描述,并封装成对象.Exception和Error的子类名都是以父类名作为后缀名. ...
- hdu4641-K-string(后缀自动机)
Problem Description Given a string S. K-string is the sub-string of S and it appear in the S at leas ...
- You don't have permission to access / on this server for debian_8
Forbidden You don't have permission to access / on this server. Apache/2.4.10 (Debian) Server at www ...
- PHP数据结构:栈、队列、堆、固定数组
数据结构:栈 队列: 堆: 固定尺寸的数组:
- JAVA并发实现三(线程的挂起和恢复)
package com.subject01; /** * 通过标识位,实现线程的挂起和回复 * com.subject01.AlternateSuspendResume.java * @author ...
- JPA字段映射(uuid,日期,枚举,@Lob)
转:http://www.cnblogs.com/tazi/archive/2012/01/04/2311588.html 主键: JPA主键的生成策略不像Hibernate那么丰富. @Id @Ge ...
- HttpServletRequest对象方法的用法(转)
原文地址:http://blog.csdn.net/xh16319/article/details/8450715 原文作者:ITSTAR 文章太赞,忍不住就想转..... 1. 获得客户机信息 ...
- IT痴汉的工作现状25-技术之养成
要想成为技术大牛,除了天赋以外,更与后天的刻苦努力分不开.伟仔我天生愚顿.工作多年后仍与大牛相差甚远,更加觉得技术的养成是一个异常困难的过程. 是我不用功吗?我不这样觉得.伟仔尽管是个懒人,但对于技术 ...
- Android字数限制的EditText实现方案研究
在应用开发中,有时需要实现有字数限制的EditText,首先来分析下市面上存在的类似实现方案吧,好有个感性的认识. [方案一:腾讯微博] 每个中文字符算一个字数,每两个英文字符算一个字数,当用户输入内 ...
- 变量a的定义
1.一个整型数 int a; 2.一个指向整型数的指针 int *a; 3.一个指向指针的指针,它指向的指针是指向一个整形数 int **a; 4.一个有10个整型数的数组 ]; 5.一个有10个指针 ...