使用代理和block写一个alertView
代理:
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的更多相关文章
- iOS:用Block写一个链式编程
一.介绍 链式编程是一个比较新颖的编程方式,简单直观,用起来也比较舒服.目前比较有名的Mansory和BabyBlueTooth就是使用链式编程写的第三方框架. 二.写法 链式编程写法不同于传统方式, ...
- 代理和 block 传值的使用
// // ZYViewController.h // BlockTest // // Created by yejiong on 14/11/2. // Copyright © 2014年 zzz. ...
- Swift基础--通知,代理和block的使用抉择以及Swift中的代理
什么时候用通知,什么时候用代理,什么时候用block 通知 : 两者关系层次太深,八竿子打不着的那种最适合用通知.因为层级结构深了,用代理要一层一层往下传递,代码结构就复杂了 代理 : 父子关系,监听 ...
- iOS中的代理和Block
一.代理(Delegate) 1)含义 iOS中的代理,比如父母要去上班,到中午12点了,需要给宝宝喂饭吃,但是父母正在上班,这时需要有一个人来帮忙完成一些事情(需要有个保姆来帮忙给宝宝喂饭),此时, ...
- 代理和block反向传值
代理传值: // SendViewController.h #import <UIKit/UIKit.h> @protocol SendInFor <NSObject> -(v ...
- 写一个基于NSURLSession的网络下载库
前段时间AFNetworking 更新到3.0 ,彻底废弃NSURLConnection的API转由NSURLSession来实现,代码并没有改动很大,AF封装的很好了,读完源码感觉收获很大. 下载不 ...
- 通过一个工具类更深入理解动态代理和Threadlocal
动态代理和Threadlocal 一个代理类返回指定的接口,将方法调用指定的调用处理程序的代理类的实例.返回的是一个代理类,由指定的类装载器的定义和实现指定接口指定代理实例调用处理程序最近用到一个工具 ...
- 从 0 开始手写一个 Mybatis 框架,三步搞定!
阅读本文大概需要 3 分钟. MyBatis框架的核心功能其实不难,无非就是动态代理和jdbc的操作,难的是写出来可扩展,高内聚,低耦合的规范的代码. 本文完成的Mybatis功能比较简单,代码还有许 ...
- 总结两种动态代理jdk代理和cglib代理
动态代理 上篇文章讲了什么是代理模式,为什么用代理模式,从静态代理过渡到动态代理. 这里再简单总结一下 什么是代理模式,给某个对象提供一个代理对象,并由代理对象控制对于原对象的访问,即客户不直接操控原 ...
随机推荐
- K米点歌APP评测
K米APP评测 产品简介 K米点歌是一款免费的社交K歌手机应用,其手机点歌功能主要在KTV.夜总会,酒吧等K歌场所中使用,当前提供iPhone版本及安卓版本下载使用.——百度百科 评测版本 K米点歌4 ...
- Day2-python基础2
本次学习内容 1.for循环 2.continue and break 3.while循环 4.运算符 5.数据类型 数字 字符串 列表 1.for循环 猜年龄的游戏完善下,只有三次机会 for i ...
- 各大主流.Net的IOC框架性能测试比较
Autofac下载地址:http://code.google.com/p/autofac/ Castle Windsor下载地址:http://sourceforge.net/projects/cas ...
- C/C++学习链接
C/C++堆和栈的区别:http://blog.csdn.net/hairetz/article/details/4141043
- 【转~】初识贝塞尔曲线(Bézier curve)
本文图文大多转自http://www.html-js.com/article/1628 QAQ我居然去扒维基,,,看不懂啊,,,我要去补数学,,, 在做变形小鸡的时候用到CSS3 transition ...
- ASP.NET AJAX调用 WebService
同事的代码,帮忙修改的,为了实现页面跳转回来后,状态的保持,Service 使用了Session. 主要的JS $.ajax({ url: "/ws/StaffInfo.asmx/Note& ...
- cf126b(kmp好题)
http://codeforces.com/contest/126/problem/B #include<bits/stdc++.h> using namespace std; const ...
- and or bool and a or b 原理解释
python 中的and从左到右计算表达式,若所有值均为真,则返回最后一个值,若存在假,返回第一个假值. or也是从左到有计算表达式,返回第一个为真的值. 代码如下: IDLE 1.2.4>&g ...
- sql select 综合运用
1 : 多个表联合查询 select j.id, jt.Name, j.ApproveType , j.ProductCode, j.Cu ...
- VMware中的Ubuntu网络设置
网络配置: VMware安装后会有两个默认网卡,分别是VMnet8(192.168.83.1)和VMnet1(192.168.19.1),当然不同的机器上,这两个网卡的 IP会不同的.在windows ...