iOS-设计模式之Block
Block是代码块,
Block定义
返回值 (^ 块名)(参数1,参数2…);
在定义Block的时候可以使用typedef 重命名一下。
typedef void(^blockName)(NSString *string);
Block和函数的相似性:
(1)可以保存代码
(2)有返回值
(3)有形参
(4)调用方式一样。
具体实现过程:
// SecondViewController.h
#import <UIKit/UIKit.h> typedef void(^blockName)(NSString *string); @interface SecondViewController : UIViewController @property (nonatomic, copy)blockName sendBlock; @end // SecondViewController.m
- (IBAction)actionTow:(id)sender {
self.sendBlock(self.textfile.text);
[self dismissViewControllerAnimated:YES completion:nil];
} // ViewController.m
#import "ViewController.h"
#import "SecondViewController.h" @interface ViewController ()
@property (weak, nonatomic) IBOutlet UILabel *lable;
@property (strong, nonatomic)SecondViewController *secondVC; @end @implementation ViewController - (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.secondVC = [[SecondViewController alloc]init];
// ARC情况下解决循环引用的方式: _weak
__weak ViewController *weakThis = self;
self.secondVC.sendBlock = ^(NSString *string){
// 如果要访问属性 还要使用__strong 来修饰。
__strong ViewController *strongThis = weakThis;
strongThis.lable.text = string;
};
}
- (IBAction)actionOne:(id)sender {
[self presentViewController:self.secondVC animated:YES completion:nil];
}
本文GitHub地址https://github.com/zhangkiwi/iOS_SN_Block
iOS-设计模式之Block的更多相关文章
- IOS设计模式之一(MVC模式,单例模式)
iOS 设计模式-你可能已经听说过这个词,但是你真正理解它意味着什么吗?虽然大多数的开发者可能都会认为设计模式是非常重要的,然而关于设计模式这一主题的文章却不多,并且有时候我们开发者在写代码的时候也不 ...
- iOS设计模式(02):单例模式
iOS设计模式(02):单例模式 singleton-design-pattern 什么是单例模式? 单例模式是一个类在系统中只有一个实例对象.通过全局的一个入口点对这个实例对象进行访问.在iOS开发 ...
- iOS 设计模式
很赞的总结 iOS Design Patterns 中文版 IOS设计模式之一(MVC模式,单例模式) IOS设计模式之二(门面模式,装饰器模式) IOS设计模式之三(适配器模式,观察者模式) IOS ...
- IOS中的Block与C++11中的lambda
ios中的block 可以说是一种函数指针,但更确切的讲,其实际上其应该算是object-c对C++11中lambda的支持或者说是一个语言上的变体,其实际内容是一样的,C++的lambda我已经有简 ...
- iOS开发关于Block代码错误
本文永久地址为http://www.cnblogs.com/ChenYilong/p/4052362.html ,转载请注明出处. iOS开发关于Block代码错误 Incompatible bloc ...
- iOS 中使用Block时需要注意的retain circle
现在在ios中,block是越来越多了.自己在类中定义block对象时,需要注意block对象的使用方法,防止产生retain circle,导致内存泄露. 现在分析一下产生retain circle ...
- iOS 设计模式之工厂模式
iOS 设计模式之工厂模式 分类: 设计模式2014-02-10 18:05 11020人阅读 评论(2) 收藏 举报 ios设计模式 工厂模式我的理解是:他就是为了创建对象的 创建对象的时候,我们一 ...
- iOS设计模式之生成器
iOS设计模式之生成器 1.生成器模式的定义 (1): 将一个复杂的对象的构件与它的表示分离,使得相同的构建过程能够创建不同的表示 (2): 生成器模式除了客户之外还包括一个Director(指导者) ...
- IOS设计模式之三:MVC模式
IOS设计模式之三:MVC模式 模型-视图-控制器 这个模式其实应该叫做MCV,用控制器把model与view隔开才对,也就是model与view互相不知道对方的存在,没有任何瓜葛,他们就像一个团 ...
- iOS 开发之Block
iOS 开发之Block 一:什么是Block.Block的作用 UI开发和网络常见功能的实现回调,按钮事件的处理方法是回调方法. 1. 按钮事件 target action 机制. 它是将一 ...
随机推荐
- Access restriction: The type * is not accessible due to restrict,报错问题
解决方案1: Eclipse 默认把这些受访问限制的API设成了ERROR. Windows -> Preferences -> Java -> Compiler -> Er ...
- iOS RGB颜色封装
使用类别创建 .h文件 #import <UIKit/UIKit.h> @interface UIColor (HexColor) + (UIColor *)colorWithHex:(N ...
- 关于JDBC中Class.forName的疑惑
一直以来都不知道为什么执行了 Class.forName(); 之后,通过DriverManager.getConnection(); 就可以获取相关数据库的连接Connection的实现呢?今天看了 ...
- CSS定义网页滚动条
(一)滚动条样式主要涉及到如下CSS属性: overflow属性: 检索或设置当对象的内容超过其指定高度及宽度时如何显示内容overflow: auto; 在需要时内容会自动添加滚动条overflow ...
- uva201 Squares
Squares A children's board game consists of a square array of dots that contains lines connecting ...
- Linux - How To Set Up an NFS Mount on CentOS 6
About NFS (Network File System) Mounts NFS mounts work to share a directory between several servers. ...
- 升级10.11后使用CocoaPod出现-bash: pod: command not found 解决办法-备
升级10.11后,运行pod命令出现: -bash: pod: command not found 解决办法: sudo gem install -n /usr/local/bin cocoapods ...
- Android 开发中关于layoutinflater
Inflater英文意思是膨胀,在Android中应该是扩展的意思吧. LayoutInflater的作用类似于 findViewById(),不同点是LayoutInflater是用来找layout ...
- ucfirst() strtoupper() strtolower()
ucfirst 将字符串第一个字符改大写. 语法: string ucfirst(string str); 返回值: 字符串 函数种类: 资料处理 内容说明 本函数返回字符串 str 第一个字的字 ...
- 在wp中,使用NavigationService.Navigate导航页面出现错误
我们在WP项目中采用页面导航时候,经常会使用以下代码 NavigationService.Navigate(new Uri("/Page1.xaml",UriKind.Relati ...