iOS代理
2016-05-23 21:06
507 查看
转载请注明出处
http://blog.csdn.net/pony_maggie/article/details/25655443
作者:小马
代理和协议的语法这里不赘述,自己查资料。
这个demo的思路是这样的,有一个A类,这个类不是一个基于视图类,它继承自NSObject,这个类会启动一个定时器,当定时器触发时,它会触发B视图弹出一个alert提醒。因为A类没法直接操作B视图,所以它用委托机制,“委托”B视图来操作。
新建一个view的工程,名为DelegateDemo,默认生成的这个视图就是我们的B视图。然后新建一个timeControl类,作为我们的A类。
A类的头文件先要定义一个协议,这个我们的代理要遵循的协议,然后应该还有一个公共的方法,用来启动定时器,代码如下:
[objc] view
plain copy
#import <Foundation/Foundation.h>
//协议定义
@protocol UpdateAlertDelegate <NSObject>
- (void)updateAlert;
@end
@interface TimerControl : NSObject
//遵循协议的一个代理变量定义
@property (nonatomic, weak) id<UpdateAlertDelegate> delegate;
- (void) startTheTimer;
@end
然后我们看看A类的实现文件,非常简单,启动定时器,定时器触发就通过代理对象更新视图:
[objc] view
plain copy
@implementation TimerControl
- (void) startTheTimer
{
[NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(timerProc) userInfo:nil repeats:NO];
}
- (void) timerProc
{
[self.delegate updateAlert];//代理更新UI
}
@end
再来看看视图类,它首先要遵循上面定义的协议,才能”帮助”A类来处理事情,如下:
[objc] view
plain copy
#import <UIKit/UIKit.h>
#import "TimerControl.h"
@interface DelegateDemoViewController : UIViewController<UpdateAlertDelegate>
@end
很明显,协议在这里就像中间人的作用,没有这个中间人,就无法”受理代理”。注意代理和协议并不是总要一起实现,只是大部分情况下我们会用协议来辅助实现代理。B视图的实现文件也很简单:
[objc] view
plain copy
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TimerControl *timer = [[TimerControl alloc] init];
timer.delegate = self; //设置代理实例
[timer startTheTimer];//启动定时器,定时5触发
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//"被代理对象"实现协议声明的方法,由"代理对象"调用
- (void)updateAlert
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
alert.alertViewStyle=UIAlertViewStyleDefault;
[alert show];
}
源码下载地址:
https://github.com/pony-maggie/DelegateDemo
http://blog.csdn.net/pony_maggie/article/details/25655443
作者:小马
代理和协议的语法这里不赘述,自己查资料。
这个demo的思路是这样的,有一个A类,这个类不是一个基于视图类,它继承自NSObject,这个类会启动一个定时器,当定时器触发时,它会触发B视图弹出一个alert提醒。因为A类没法直接操作B视图,所以它用委托机制,“委托”B视图来操作。
新建一个view的工程,名为DelegateDemo,默认生成的这个视图就是我们的B视图。然后新建一个timeControl类,作为我们的A类。
A类的头文件先要定义一个协议,这个我们的代理要遵循的协议,然后应该还有一个公共的方法,用来启动定时器,代码如下:
[objc] view
plain copy
#import <Foundation/Foundation.h>
//协议定义
@protocol UpdateAlertDelegate <NSObject>
- (void)updateAlert;
@end
@interface TimerControl : NSObject
//遵循协议的一个代理变量定义
@property (nonatomic, weak) id<UpdateAlertDelegate> delegate;
- (void) startTheTimer;
@end
然后我们看看A类的实现文件,非常简单,启动定时器,定时器触发就通过代理对象更新视图:
[objc] view
plain copy
@implementation TimerControl
- (void) startTheTimer
{
[NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(timerProc) userInfo:nil repeats:NO];
}
- (void) timerProc
{
[self.delegate updateAlert];//代理更新UI
}
@end
再来看看视图类,它首先要遵循上面定义的协议,才能”帮助”A类来处理事情,如下:
[objc] view
plain copy
#import <UIKit/UIKit.h>
#import "TimerControl.h"
@interface DelegateDemoViewController : UIViewController<UpdateAlertDelegate>
@end
很明显,协议在这里就像中间人的作用,没有这个中间人,就无法”受理代理”。注意代理和协议并不是总要一起实现,只是大部分情况下我们会用协议来辅助实现代理。B视图的实现文件也很简单:
[objc] view
plain copy
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
TimerControl *timer = [[TimerControl alloc] init];
timer.delegate = self; //设置代理实例
[timer startTheTimer];//启动定时器,定时5触发
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//"被代理对象"实现协议声明的方法,由"代理对象"调用
- (void)updateAlert
{
UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"提示" message:@"时间到" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定",nil];
alert.alertViewStyle=UIAlertViewStyleDefault;
[alert show];
}
源码下载地址:
https://github.com/pony-maggie/DelegateDemo
相关文章推荐
- 记一次事件委托在 ios 下的兼容 bug
- 【开发工具】iOS真机调试抓包工具Charles安装及使用
- iOS - NSDate与NSString的相互转换
- iOS中常用的四种数据持久化方法
- iOS 视图控制器转场详解
- ios 修改导航栏的颜色
- opencv-ios开发笔记3 直方图匹配
- iOS更改tabbar图片渲染 —不让tabbat有蓝色的渲染 并修改文字
- iOS 开发编程你需要知道的知识(不定期更新) —— HERO博客
- ios图片添加水印
- iOS Xcode编程常用快捷键 —— HERO博客
- iOS tableView表视图设置背景图片
- iOS线程通信和进程通信的例子(NSMachPort和NSTask,NSPipe)
- iOS的粒子系统
- iOS开发之直播App流程介绍,直播资料收集汇总,视频推流,视频拉流,SMTP、RTMP、HLS、 PLPlayerKit
- 郑州iOS點 - 點哥教你去掉Tabbar上沿阴影线
- iOS五角星绘制
- iOS 9 Auto Layout界面自动布局系列6-自适应布局
- iOS开发——毛玻璃透明
- iOS中多线程原理与runloop介绍