重写AlertView(用block)
@interface AlertView : UIView
@property (nonatomic,copy) void(^block)(UIColor *color);
- (id)initWithAlertView;
- (void)showTwo;
@end
自定义View的.m文件
- (id)initWithAlertView
{
self = [super init];//自定义init方法 就是重写了系统的init方法;
if (self)
{
UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
btn1.backgroundColor = [UIColor redColor];
btn1.frame = CGRectMake(5, 100, 40, 25);
[btn1 addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn1];
UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
btn2.backgroundColor = [UIColor greenColor];
btn2.frame = CGRectMake(150, 100, 40, 25);
[btn2 addTarget:self action:@selector(btn2Click:) forControlEvents:UIControlEventTouchUpInside];
[self addSubview:btn2];
}
return self;
}
- (void)btn1Click:(UIButton *)btn
{
self.block(btn.backgroundColor);
[self removeFromSuperview];
}
- (void)btn2Click:(UIButton *)btn
{
self.block(btn.backgroundColor);
[self removeFromSuperview];
}
主界面的.m
- (IBAction)buttonClick:(id)sender
{
AlertView *alertView = [[AlertView alloc] initWithAlertView];
alertView.backgroundColor = [UIColor grayColor];
alertView.frame = CGRectMake(100, 100, 200, 130);
// [alertView initWithAlertView];
[self.view addSubview:alertView];
__block ViewController *vc = self;
alertView.block = ^(UIColor *color){
vc.view.backgroundColor = color;
};
// [alertView showTwo];要不要都行 看情况而定
}
重写AlertView(用block)的更多相关文章
- 用block响应button的点击事件
1.继承UIButton : 2.在自己定义的button类中的方法 addTarget:(id)target action:(SEL)action forControlEvents:(UIContr ...
- django模板复用 extends,block,include
template复用 extends block include render 参考:https://code.ziqiangxuetang.com/django/django-template.ht ...
- Django模板之模板继承(extends/block)
Django模版引擎中最强大也是最复杂的部分就是模版继承了.模版继承可以让您创建一个基本的“骨架”模版,它包含您站点中的全部元素,并且可以定义能够被子模版覆盖的 block. 模板继承: 1. ...
- Object-c Associated Object
oc的关联的作用在我看来就是将两个对象关联起来,用的时候通过key和对象把和这个对象关联的对象再取出来(我做的项目就是和UITableView里面的一个属性关联起来了) 举个栗子: - (void)v ...
- 【读书笔记】--《编写高质量iOS与OS X代码的52个有效方法》
1.Objective-C 起源: 在 C 语言基础上添加了面向对象特性,是 C 语言的超集.Objective-C 由 SmallTalk 语言演变过来,使用消息结构,运行环境由运行环境决定. OC ...
- Django模板的继承
一.extend 1.extend继承模板 2.一个文件中只能继承一个模板 3.extend继承模板中的所有内容,模板的内容包括:html的head和body ,eg:
- Effective Objective-C 2.0 — 第10条:在既有类中使用关联对象存放自定义数据
可以通过“关联对象”机制来把两个对象连起来 定义关联对象时可指定内存管理语义,用以模仿定义属性时所采用的“拥有关系”与“非拥有关系” 只有在其他做法不可行时才应选用关联对象,因为这种做法通常会引入难于 ...
- PHP模版引擎 – Twig
在网站开发过程中模版引擎是必不可少的,PHP中用的最多的当属Smarty了.目前公司系统也是用的Smarty,如果要新增一个页面只需把网站的头.尾和左侧公共部分通过Smarty的include方式引入 ...
- 编写高质量iOS代码与OS X代码的effective 方法小结
一.熟悉OC: 了解OC的起源: OC和C++,Java等面向对象语言类似,不过有很方面差别.因为该语言使用 消息结构而非函数调用. 消息结构和函数调用的区别:前者是在其运行时所应执行的代码由运行环 ...
随机推荐
- Google科学家前腾讯副总裁吴军将出席第二届万物互联创新大会
当越来越多的科技产品注入互联网的基因,"万物互联"的模式悄然兴起.第二届万物互联创新大会(B12大会)将于2016-11-13日在杭州市余杭区隆重召开.Google科学家前腾讯副总 ...
- react中文API解读二(教程)
记下自己的react学习之路 ,官方文档写的很详尽,学起来应该比较简单 官方文档地址:react.http://reactjs.cn/react/docs/getting-started.html 2 ...
- C库 - 常用文件IO函数
#include<stdio.h> 0. 文件打开关闭FILE *fp = fopen("C:\\a.dat","wb+");fclose(fp); ...
- memset用法总结
memset是计算机中C/C++语言函数.将s所指向的某一块内存中的前n个 字节的内容全部设置为ch指定的ASCII值, 第一个值为指定的内存地址,块的大小由第三个参数指定,这个函数通常为新申请的内存 ...
- ZIP文件解压
public class DZip { /// <summary> /// 压缩为ZIP文件 /// </summary> public void Zip(string dir ...
- css sprite的使用心得
提高网页显示速度最有效的一个方法是减少页面的HTTP请求次数,为了减少HTTP请求次数,最直接有效的方法是使用精灵图片(CSS sprites),精灵图片是把许多图片放到一张大图片里面,通过CSS来显 ...
- sort 排序详解
http://www.qdfuns.com/notes/17398/c56832f62b3271e79671f77dcdd2c058.html. http://www.qdfuns.com/notes ...
- margin的auto的理解
auto 总是试图充满父元素 margin有四个值: All the margin properties can have the following values: auto - the brows ...
- Python中exec的使用
>>>globals = {'x':7, .....: 'y':10, .....: 'names':['aa','bb','cc'] .....: } >>>lo ...
- mybatis框架源码学习
转:来自https://my.oschina.net/u/1458864/blog/293659 摘要:初始化mybatis,所有的配置都在configuation 对象中使用mybatis,从sql ...