//委托的协议定义
@protocol UpdateDelegate <NSObject>
- (void)update;
@end @interface Test : NSObject
//委托变量定义
@property (nonatomic, weak) id<UpdateDelegate> delegate; //block
typedef void (^UpdateBlock)(id obj);
@property (nonatomic, copy) UpdateBlock updateBlock; - (void) startTest; @end @implementation Test - (void) startTest
{ [NSTimer scheduledTimerWithTimeInterval:3.0f target:self selector:@selector(scheduledEnd) userInfo:nil repeats:NO];
} - (void)scheduledEnd
{
[self.delegate update];//委托 //block
if (self.updateBlock)
{
self.updateBlock(@"test");
}
} @end // 实现
- (void)viewDidLoad
{
[super viewDidLoad]; Test *test = [[Test alloc] init];
test.delegate = self; //设置委托实例 //实现block
test.updateBlock = ^(id obj)
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:obj delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil]; alert.alertViewStyle=UIAlertViewStyleDefault;
[alert show];
}; [test startTest];//启动定时器
} //"被委托对象"实现协议声明的方法,由"委托对象"调用
- (void)update
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil]; alert.alertViewStyle=UIAlertViewStyleDefault;
[alert show];
}

ios delegate 和 block的更多相关文章

  1. iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例)

    iOS页面间传值的方式(NSUserDefault/Delegate/NSNotification/Block/单例) 实现了以下iOS页面间传值:1.委托delegate方式:2.通知notific ...

  2. 【转】iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)-- 不错

    原文网址:http://www.cnblogs.com/JuneWang/p/3850859.html iOS页面间传值的方式(NSUserDefault/Delegate/NSNotificatio ...

  3. iOS页面间传值的方式 (Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例)   iOS页面间传值的方式(NSUserDefault/Delegate/NSN ...

  4. iOS页面间传值的五种方式总结(Delegate/NSNotification/Block/NSUserDefault/单例)

    iOS页面间传值的方式(Delegate/NSNotification/Block/NSUserDefault/单例) iOS页面间传值的方式(NSUserDefault/Delegate/NSNot ...

  5. iOS 键盘添加完成按钮,delegate和block回调

    这个是一个比较初级一点的文章,新人可以看看.当然实现这个需求的时候自己也有一点收获,记下来吧. 前两天产品要求在工程的所有数字键盘弹出时,上面带一个小帽子,上面安装一个“完成”按钮,这个完成按钮也没有 ...

  6. Swift 用Delegate和Block实现回调的Demo

    一.有关回调 我们知道,执行函数的时候,一般都有return作为返回参数了,那有return了为什么还要回调呢? 回调是为了实现异步的返回,在某些特殊的情况下,比如你执行的函数是一个长时间运行的函数, ...

  7. iOS中使用block进行网络请求回调

    iOS中使用block进行网络请求回调 HttpRequest.h // // HttpRequest.h // UseBlockCallBack // // Created by Michael o ...

  8. IOS中的Block与C++11中的lambda

    ios中的block 可以说是一种函数指针,但更确切的讲,其实际上其应该算是object-c对C++11中lambda的支持或者说是一个语言上的变体,其实际内容是一样的,C++的lambda我已经有简 ...

  9. iOS开发关于Block代码错误

    本文永久地址为http://www.cnblogs.com/ChenYilong/p/4052362.html ,转载请注明出处. iOS开发关于Block代码错误 Incompatible bloc ...

随机推荐

  1. [Cycle.js] Main function and effects functions

    We need to give structure to our application with logic and effects. This lessons shows how we can o ...

  2. lseek() 定位一个已经打开的文件

    Lseek lseek()的作用是,设置文件内容的读写位置. 每个打开的文件都有一个"当前文件偏移量",是一个非负整数,用以度量从文件开始处计算的字节数.通常,读写操作都是从当前文 ...

  3. CSS background 属性 总结

    CSS background 属性总结

  4. 从反编译的角度去观察C#6.0

    1. 自动属性初始化 (Initializers for auto-properties) 1.1 C#6.0 之前的写法 public class FirstExperience { private ...

  5. 点击<a>标签,禁止页面自动跳到顶部的解决办法

       最近在开发一个小web的时候想给一个按钮增加一个弹出dialog功能,但是发现点击按钮后页面总是自动滚动至顶部,这点从用户体验上来讲是极其不爽的,于是开始跳进google大池寻求解决办法.网上的 ...

  6. 《第一行代码》学习笔记4-活动Activity(2)

    1.Toast是Android系统中一种好的提醒方式,程序中使用它将一些短小的信 息通知给用户,信息会在不久自动消失,不占用任何屏幕空间. 2.定义一个弹出Toast的出发点,界面有按钮,就让点击按钮 ...

  7. shell中的type命令

    type [-aftpP] name [name,...] -a 打印name的所有可能情况,比如type -a ls,会打印ls is aliased to 'ls --color=auto'和ls ...

  8. nodejs 保存 payload 发送过来的文件

    1:接受文件 http://stackoverflow.com/questions/24610996/how-to-get-uploaded-file-in-node-js-express-app-u ...

  9. window下配置ssh key

    在windows下通过msysGit(Git for windows.Git Bash)配置SSH Keys连接GitHub. 1.检查本机是否有ssh key设置 $ cd ~/.ssh 或cd . ...

  10. YII学习笔记-登录后的session的总结

    在YII框架的默认的登录后的session数据是id,name,__states这三个数据. 在搭配好YII框架环境后,可以使用admin/admin,来登录系统.如果在protected/views ...