@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)的更多相关文章

  1. 用block响应button的点击事件

    1.继承UIButton : 2.在自己定义的button类中的方法 addTarget:(id)target action:(SEL)action forControlEvents:(UIContr ...

  2. django模板复用 extends,block,include

    template复用 extends block include render 参考:https://code.ziqiangxuetang.com/django/django-template.ht ...

  3. Django模板之模板继承(extends/block)

    Django模版引擎中最强大也是最复杂的部分就是模版继承了.模版继承可以让您创建一个基本的“骨架”模版,它包含您站点中的全部元素,并且可以定义能够被子模版覆盖的 block. 模板继承: 1.    ...

  4. Object-c Associated Object

    oc的关联的作用在我看来就是将两个对象关联起来,用的时候通过key和对象把和这个对象关联的对象再取出来(我做的项目就是和UITableView里面的一个属性关联起来了) 举个栗子: - (void)v ...

  5. 【读书笔记】--《编写高质量iOS与OS X代码的52个有效方法》

    1.Objective-C 起源: 在 C 语言基础上添加了面向对象特性,是 C 语言的超集.Objective-C 由 SmallTalk 语言演变过来,使用消息结构,运行环境由运行环境决定. OC ...

  6. Django模板的继承

    一.extend 1.extend继承模板 2.一个文件中只能继承一个模板 3.extend继承模板中的所有内容,模板的内容包括:html的head和body ,eg:

  7. Effective Objective-C 2.0 — 第10条:在既有类中使用关联对象存放自定义数据

    可以通过“关联对象”机制来把两个对象连起来 定义关联对象时可指定内存管理语义,用以模仿定义属性时所采用的“拥有关系”与“非拥有关系” 只有在其他做法不可行时才应选用关联对象,因为这种做法通常会引入难于 ...

  8. PHP模版引擎 – Twig

    在网站开发过程中模版引擎是必不可少的,PHP中用的最多的当属Smarty了.目前公司系统也是用的Smarty,如果要新增一个页面只需把网站的头.尾和左侧公共部分通过Smarty的include方式引入 ...

  9. 编写高质量iOS代码与OS X代码的effective 方法小结

    一.熟悉OC: 了解OC的起源: OC和C++,Java等面向对象语言类似,不过有很方面差别.因为该语言使用  消息结构而非函数调用. 消息结构和函数调用的区别:前者是在其运行时所应执行的代码由运行环 ...

随机推荐

  1. win32api大全

    win32api大全 http://files.cnblogs.com/files/landv/Win32API%E5%A4%A7%E5%85%A8.zip

  2. POJ 3070 矩阵快速幂解决fib问题

    矩阵快速幂:http://www.cnblogs.com/atmacmer/p/5184736.html 题目链接 #include<iostream> #include<cstdi ...

  3. 2016NEFU集训第n+3场 D - Bicycle Race

    Description Maria participates in a bicycle race. The speedway takes place on the shores of Lake Luc ...

  4. tab一些 添加 删除 搜索

    tab一些 添加 删除 搜索 案例 <!DOCTYPE html><html lang="en"><head> <meta charset ...

  5. Openjudge-计算概论(A)-能被3,5,7整除的数

    描述: 输入一个整数,判断它能否被3,5,7整除,并输出以下信息:1.能同时被3,5,7整除(直接输出3 5 7,每个数中间一个空格):2.能被其中两个数整除(输出两个数,小的在前,大的在后.例如:3 ...

  6. ural 1203. Scientific Conference(动态规划)

    1203. Scientific Conference Time limit: 1.0 second Memory limit: 64 MB Functioning of a scientific c ...

  7. onbeforeunload与a标签在IE中的冲突bug(转载)

    onbeforeunload与a标签在IE中的冲突bug   onbeforeunload 是window的一个事件,目前Firefox,IE都支持,主要用来提示用户是否真的要离开该页面,通常在一些比 ...

  8. Tennis Championship

    Tennis Championship time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  9. WEB 端批量移动设备管理控制工具 STF 的环境搭建和运行

    最近项目涉及到较多设备批量管理的需求,发现一工具,可以批量对大量设备进行WEB端管理,工具主页:https://openstf.github.io/工具名STF(Smartphone Test Far ...

  10. Chapter 2 Open Book——12

    I called him in when dinner was ready, and he sniffed appreciatively as he walked into the room. 当晚饭 ...