UI中的协议使用步骤
2015-08-03 18:11
183 查看
创建一个类继承于UIView
.h中的步骤
// (1).声明一份协议
@protocol MyButtonDelegate <NSObject>
-(void)changeColor;
@end
@interface MyButton :
UIView
(2).设置代理人的属性
@property(nonatomic,
assign)id<MyButtonDelegate>delegate;
(3)在MyButton.m里
@end
.m里
// (3).重写触发方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
*)event
{
(4).设置代理人执行的方法
[self.delegate
changeColor];
(5)在MainViewController.m里
}
MainViewController.m
@interface
MainViewController ()<MyButtonDelegate>//
注意这个地方*****
(5).引完头文件之后
定义
MyButton *button = [[MyButton
alloc]initWithFrame:CGRectMake(100,
100, 100,
40)];
button.backgroundColor = [UIColor
yellowColor];
[self.view
addSubview:button];
[button release];
(6).设置代理人
button.delegate =
self;
-(void)changeColor
{
self.view.backgroundColor = [UIColor
colorWithRed:arc4random() %
256 / 255.0
green:arc4random() %
256 / 255.0
blue:arc4random() %
256 / 255.0
alpha:arc4random() %
256 / 255.0];
}
.h中的步骤
// (1).声明一份协议
@protocol MyButtonDelegate <NSObject>
-(void)changeColor;
@end
@interface MyButton :
UIView
(2).设置代理人的属性
@property(nonatomic,
assign)id<MyButtonDelegate>delegate;
(3)在MyButton.m里
@end
.m里
// (3).重写触发方法
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent
*)event
{
(4).设置代理人执行的方法
[self.delegate
changeColor];
(5)在MainViewController.m里
}
MainViewController.m
@interface
MainViewController ()<MyButtonDelegate>//
注意这个地方*****
(5).引完头文件之后
定义
MyButton *button = [[MyButton
alloc]initWithFrame:CGRectMake(100,
100, 100,
40)];
button.backgroundColor = [UIColor
yellowColor];
[self.view
addSubview:button];
[button release];
(6).设置代理人
button.delegate =
self;
-(void)changeColor
{
self.view.backgroundColor = [UIColor
colorWithRed:arc4random() %
256 / 255.0
green:arc4random() %
256 / 255.0
blue:arc4random() %
256 / 255.0
alpha:arc4random() %
256 / 255.0];
}
相关文章推荐
- String StringBuffer StringBuilder比较
- iOS之自定义导航按钮UIBarButtonItem的样式
- easyUI的combox三级联动
- 56.UICollectionView的基本使用
- UITarget-Action(定义一个触碰屏幕启动功能的方法)
- hdu 1711 Number Sequence(求模式串首次出现在文本串的位置)
- UISwitch
- UITouch(事件)及其如何让视图随着鼠标移动
- Implement Queue using Stacks
- UGUI 使用 BMFont
- leetcode-225-Implement Stack using Queues
- Implement Stack using Queues
- easyui-accordion动态生成菜单的实现
- leetcode-232-Implement Queue using Stacks
- Hbuilder主页面控制子页面的方法
- UVA 11572 Unique snowflakes (滑窗)
- UI_Delegate
- iOS开发UI篇—在UItableview中实现加载更多功能
- swirl 1: Basic Building Blocks
- hdu 5312 Sequence