美文网首页
iOS Block 常用的两种(属性)和(参数)

iOS Block 常用的两种(属性)和(参数)

作者: 兰歌er | 来源:发表于2017-11-02 15:27 被阅读13次

Block是一种比较特殊的数据类型。它可以保存一段代码,在合适的时候取出来调用。

eg:

int (^SumOfNumbers)(int a,int b) = ^(int a,int b){

return a+b;

};

就像这张图:

Block

用作属性的例子:

点击ViewController1中的Button,跳转到ViewController2中

在ViewController2中的TextField中输入字符串

后点击ViewController2中的button返回到ViewController1

ViewController1中的label显示我们输入的字符串

1 2 3 4 5 6

demo链接:小链接

用作参数的例子:

自己写一个方法:

1

然后调用这个方法

2

调用这个方法是在花括号内可以do something。

相关文章

网友评论

      本文标题:iOS Block 常用的两种(属性)和(参数)

      本文链接:https://www.haomeiwen.com/subject/pswopxtx.html