代理:

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. 【Beta版本】七天冲刺集结令

    031402304 陈燊 031402342 许玲玲 031402337 胡心颖 03140241 王婷婷 031402203 陈齐民 031402209 黄伟炜 031402233 郑扬涛 [Bet ...

  2. BZOJ3747: [POI2015]Kinoman

    传送门 线段树经典运用. 设$last_i$表示上一个与$i$相同的类型.然后每次更新$[last[i]+1,i]$和$[last[last[i]]+1,last[i]]$的答案就行了. //BZOJ ...

  3. Architectural Model - SNMP Tutorial

    30.3 Architectural Model Despite the potential disadvantages, having TCP/IP management software oper ...

  4. C# 自定义Section

    一.在App.config中自定义Section,这个使用了SectionGroup <?xml version="1.0" encoding="utf-8&quo ...

  5. session生命周期(一)

    Session存储在服务器端,一般为了防止在服务器的内存中(为了高速存取),Session在用户访问第一次访问服务器时创建,需要注意只有访问JSP.Servlet等程序时才会创建Session,只访问 ...

  6. hashicorp/consul

    https://github.com/hashicorp/consul/tree/master/vendor/github.com/boltdb/bolt

  7. 父类方法返回子类实例:PHP延迟静态绑定

    案例分析 先前的PHP项目中,看到类似于以下的一段代码: <?php class DBHandler { public function get() { } } class MySQLHandl ...

  8. 相同根域名下跨域共享session的解决方案

    https://code.msdn.microsoft.com/CSASPNETShareSessionBetween-021daa39

  9. windows vim修改字体

    C:\Program Files (x86)\Vim\vim74 目录下,在vimrc_example.vim和mswin.vim中添加: set guifont=Consolas:h11

  10. 进阶系列三【绝对干货】----Log4.Net的介绍

    一.介绍 当我们开发软件时,一般都会加入运行期的跟踪手段,以方便后续故障分析和Bug调试..net framework本身提供了一个System.Diagnostics.Trace类来实现流程跟踪功能 ...