UI之targetAction
2015-12-14 10:16
429 查看
用targetAction来改变view的大小和颜色
#import <UIKit/UIKit.h>
@interface MangoView : UIView
//向外部公开接口,传入目标和动作
- (void)addtarget:(id)target action:(SEL)action;
@end
#import "MangoView.h"
#import "MainViewController.h"
@interface MangoView ()
{
//target、action的属性命名方法:
id _target;//目标,将要执行动作的对象
SEL _action;//动作,目标对象将要执行的操作
}
@end
@implementation MangoView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self lodingCustomView];
}
return self;
}
- (void)lodingCustomView{
}
- (void)addtarget:(id)target action:(SEL)action{
// 把外部传入的target目标和action动作赋值给mangoView自身的目标和动作的实例变量
_target = target;
_action = action;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 当点击之后我们需要让目标去执行对应的动作
[_target performSelector:_action];
}
@end
#import "MainViewController.h"
#import "MangoView.h"
@interface MainViewController ()
@property(nonatomic,retain)MangoView *mangoView;
@property(nonatomic,retain)MangoView *mangoView1;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
self.mangoView = [[MangoView alloc]initWithFrame:CGRectMake(40, 100, 300, 100)];
self.mangoView.backgroundColor = [UIColor yellowColor];
//在实例对象中添加target和action方法
[self.mangoView addtarget:self action:@selector(changeColor)];
[self.view addSubview:self.mangoView];
[self.mangoView release];
self.mangoView1 = [[MangoView alloc]initWithFrame:CGRectMake(40, 270, 300, 150)];
self.mangoView1.backgroundColor = [UIColor greenColor];
[self.mangoView1 addtarget:self action:@selector(changeSize)];
[self.view addSubview:self.mangoView1];
[self.mangoView1 release];
}
- (void)changeColor{
self.mangoView.backgroundColor = [UIColor orangeColor];
}
- (void)changeSize{
self.mangoView1.frame = CGRectMake(40, 270, 300, 100);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
#import <UIKit/UIKit.h>
@interface MangoView : UIView
//向外部公开接口,传入目标和动作
- (void)addtarget:(id)target action:(SEL)action;
@end
#import "MangoView.h"
#import "MainViewController.h"
@interface MangoView ()
{
//target、action的属性命名方法:
id _target;//目标,将要执行动作的对象
SEL _action;//动作,目标对象将要执行的操作
}
@end
@implementation MangoView
- (instancetype)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
[self lodingCustomView];
}
return self;
}
- (void)lodingCustomView{
}
- (void)addtarget:(id)target action:(SEL)action{
// 把外部传入的target目标和action动作赋值给mangoView自身的目标和动作的实例变量
_target = target;
_action = action;
}
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 当点击之后我们需要让目标去执行对应的动作
[_target performSelector:_action];
}
@end
#import "MainViewController.h"
#import "MangoView.h"
@interface MainViewController ()
@property(nonatomic,retain)MangoView *mangoView;
@property(nonatomic,retain)MangoView *mangoView1;
@end
@implementation MainViewController
- (void)viewDidLoad {
[super viewDidLoad];
// Do any additional setup after loading the view.
self.view.backgroundColor = [UIColor cyanColor];
self.mangoView = [[MangoView alloc]initWithFrame:CGRectMake(40, 100, 300, 100)];
self.mangoView.backgroundColor = [UIColor yellowColor];
//在实例对象中添加target和action方法
[self.mangoView addtarget:self action:@selector(changeColor)];
[self.view addSubview:self.mangoView];
[self.mangoView release];
self.mangoView1 = [[MangoView alloc]initWithFrame:CGRectMake(40, 270, 300, 150)];
self.mangoView1.backgroundColor = [UIColor greenColor];
[self.mangoView1 addtarget:self action:@selector(changeSize)];
[self.view addSubview:self.mangoView1];
[self.mangoView1 release];
}
- (void)changeColor{
self.mangoView.backgroundColor = [UIColor orangeColor];
}
- (void)changeSize{
self.mangoView1.frame = CGRectMake(40, 270, 300, 100);
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
相关文章推荐
- easyui-textbox 和 easyui-validatebox 设置值和获取值
- 学习opencv(3)——cvRodrigues2()、cvMatMulAdd()
- BIOS与UEFI、MBR和GPT介绍
- maven 出现 SunCertPathBuilderException错误
- BurpSuite_Pro Ver1.6.24 最新破解版分享
- UIScrollView的自动布局最简单实现方式,不用一句代码即可滚动
- 太原UI设计师郭文军让【初学者】掌握web代码的标签及属性颜色的提示
- UI之responder
- 通过velocity模板和Hibernate sql-query的实现动态hql
- 【LEETCODE】232-Implement Queue using Stacks
- [mysql]ERROR 1364 (HY000): Field 'ssl_cipher' doesn't have a default value
- UI之touchEvent
- UIViewController的生命周期
- UIImageView添加点击事件
- 使用go build 进行条件编译
- iOS开发中UITableview控件的基本使用及性能优化方法
- 《NSUserDefaults数据存储和 UIScrollView 的解说,和引导也的创建》
- Arduino - 利用红外避障(障碍检测)进行计数
- 网页性能管理详解:浅谈chrome-Timeline及window.requestAnimationFrame()方法
- 关于android官方提供的BluetoothChat源码连接不上蓝牙适配器的问题