代理:

MyAlertView.h:
@property (nonatomic,assign)id delegate; @protocol MyAlertViewDelegate <NSObject> - (void)myAlertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex; @end
MyAlertView.m:
- (void)buttonClick:(id)sender
{
UIButton *button = (UIButton *)sender;
int index = (int)button.tag - ; if ([self.delegate respondsToSelector:@selector(myAlertView:clickedButtonAtIndex:)]) {
[self.delegate myAlertView:self clickedButtonAtIndex:index];
}
[self disappear];
}
调用处ViewController:
- (void)myAlertView:(MyAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
NSLog(@"buttonIndex:%d",buttonIndex);
}

使用block:

MyAlertView.h:

//使用copy 而不是retain ,因为block 变量是声明在栈上的,retain之后还是在栈上,方法结束,栈的内存就被系统自动释放,copy 可以把一个block 变量从栈上拷贝到堆上,堆上的内存由引用计数管理,所以我们就能自己决定block的释放。
@property (nonatomic,copy)void (^buttonClick)(int);
MyAlertView.m:
- (void)buttonClick:(id)sender
{
UIButton *button = (UIButton *)sender;
int index = (int)button.tag - ; self.buttonClick(index); [self disappear];
}
调用处ViewController:
alertView.buttonClick = ^(int index){
NSLog(@"buttonIndex:%d",buttonIndex);
};

UILabel类别自适应高度

#import <UIKit/UIKit.h>

@interface UILabel (AutoFitSize)

+(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor;

@end
#import "UILabel+AutoFitSize.h"

//判断是否是IOS7以上的系统(包含IOS7)
#define bIos7 ([[[UIDevice currentDevice] systemVersion] floatValue] >= 7.0?YES : NO) @implementation UILabel (AutoFitSize) #if __IPHONE_OS_VERSION_MAX_ALLOWED < 70000 +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor
{
label.font=kfont;
label.text = ktext;
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor=kbackgroundColor; label.numberOfLines=;
label.lineBreakMode=NSLineBreakByTruncatingTail;
//内容显示 高度自适应
CGSize sizeToFit =[ktext sizeWithFont:font constrainedToSize:CGSizeMake(krect.size.width,)lineBreakMode:NSLineBreakByWordWrapping];
label.frame=CGRectMake(krect.origin.x,krect.origin.y,sizeToFit.width, sizeToFit.height);;
return label;
} #else +(UILabel *)getInfoLabel:(UILabel *)label withText:(NSString *)ktext withFont:(UIFont *)kfont withtosize:(CGRect)krect withBackGroundColor:(UIColor *)kbackgroundColor
{
label.text = ktext;
label.font = kfont;
label.textColor = [UIColor blackColor];
label.textAlignment = NSTextAlignmentCenter;
label.backgroundColor = kbackgroundColor; label.numberOfLines =;
label.lineBreakMode =NSLineBreakByTruncatingTail ;
//高度估计文本大概要显示几行,宽度根据需求自己定义。 MAXFLOAT 可以算出具体要多高
CGSize size =CGSizeMake(krect.size.width,);
NSDictionary * tdic = [NSDictionary dictionaryWithObjectsAndKeys:kfont,NSFontAttributeName,nil];
//ios7方法,获取文本需要的size,限制宽度
CGSize actualsize =[ktext boundingRectWithSize:size options:NSStringDrawingUsesLineFragmentOrigin attributes:tdic context:nil].size;
label.frame=CGRectMake(krect.origin.x,krect.origin.y,actualsize.width, actualsize.height); return label;
} #endif @end

代码地址:http://pan.baidu.com/s/1hqAKp3u

 
 
 
 
 

使用代理和block写一个alertView的更多相关文章

  1. iOS:用Block写一个链式编程

    一.介绍 链式编程是一个比较新颖的编程方式,简单直观,用起来也比较舒服.目前比较有名的Mansory和BabyBlueTooth就是使用链式编程写的第三方框架. 二.写法 链式编程写法不同于传统方式, ...

  2. 代理和 block 传值的使用

    // // ZYViewController.h // BlockTest // // Created by yejiong on 14/11/2. // Copyright © 2014年 zzz. ...

  3. Swift基础--通知,代理和block的使用抉择以及Swift中的代理

    什么时候用通知,什么时候用代理,什么时候用block 通知 : 两者关系层次太深,八竿子打不着的那种最适合用通知.因为层级结构深了,用代理要一层一层往下传递,代码结构就复杂了 代理 : 父子关系,监听 ...

  4. iOS中的代理和Block

    一.代理(Delegate) 1)含义 iOS中的代理,比如父母要去上班,到中午12点了,需要给宝宝喂饭吃,但是父母正在上班,这时需要有一个人来帮忙完成一些事情(需要有个保姆来帮忙给宝宝喂饭),此时, ...

  5. 代理和block反向传值

    代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...

  6. 写一个基于NSURLSession的网络下载库

    前段时间AFNetworking 更新到3.0 ,彻底废弃NSURLConnection的API转由NSURLSession来实现,代码并没有改动很大,AF封装的很好了,读完源码感觉收获很大. 下载不 ...

  7. 通过一个工具类更深入理解动态代理和Threadlocal

    动态代理和Threadlocal 一个代理类返回指定的接口,将方法调用指定的调用处理程序的代理类的实例.返回的是一个代理类,由指定的类装载器的定义和实现指定接口指定代理实例调用处理程序最近用到一个工具 ...

  8. 从 0 开始手写一个 Mybatis 框架,三步搞定!

    阅读本文大概需要 3 分钟. MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码. 本文完成的Mybatis功能比较简单,代码还有许 ...

  9. 总结两种动态代理jdk代理和cglib代理

    动态代理 上篇文章讲了什么是代理模式,为什么用代理模式,从静态代理过渡到动态代理. 这里再简单总结一下 什么是代理模式,给某个对象提供一个代理对象,并由代理对象控制对于原对象的访问,即客户不直接操控原 ...

随机推荐

  1. easyUi 页面创建一个toolbar实例

    1.定义toolbar方法 pagination : true, pageSize : 10, pageList : [ 5, 10, 15, 20, 50 ], toolbar : toolbarF ...

  2. MonoGame教程

    http://www.gamefromscratch.com/page/MonoGame-Tutorial-Series.aspx http://rbwhitaker.wikidot.com/mono ...

  3. C++11特性:decltype关键字

    decltype简介 我们之前使用的typeid运算符来查询一个变量的类型,这种类型查询在运行时进行.RTTI机制为每一个类型产生一个type_info类型的数据,而typeid查询返回的变量相应ty ...

  4. 1JavaEE应用简介----青软S2SH(笔记)

    这本书主要是讲解Struts2,spring,Hibernate框架的, 因为工作中用的较多的是SpringMVC,Struts2用的较少,所以想系统学习一下,就买了这本书. 这本书是青软的,虽然是培 ...

  5. C# 读取excel日期时获取到数字转换成日期

    string strDate= DateTime.FromOADate(Convert.ToInt32(data[i][7])).ToString("d"); strDate= D ...

  6. Android之自定义属性

    有些时候会觉得Android中提供的控件不能满足项目的要求,所以就会常常去自定义控件.自定义控件就不免会自定义属性.自定义属性大致需要三个步骤:在XML文件中定义自定义属性的名称和数据类型.在布局中调 ...

  7. Private-code MaxCounter

    No need for a double cycle: : You are given N counters, initially set to 0, and you have two possibl ...

  8. Mac Pro 利用PHP导出SVN新增或修改过的文件

    先前在 Windows 操作系统下,习惯用 TortoiseSVN 导出新增或修改过的文件([相当实用]如何让TortoiseSVN导出新增或修改过的文件 ),最近换成了 Mac Pro 笔记本电脑, ...

  9. Mac Pro 编译安装 Redis-3.2.3

    Redis官方下载地址:http://redis.io/download Redis安装 cd /usr/local/src/redis-3.2.3 sudo make sudo make insta ...

  10. (备忘)Rect和RectF的区别

    1.Rect的变量使用int类型,而RectF使用float类型. 2.一些方法区别 <1>.Rect类 equals(Object obj) (for some reason it as ...