iOS UI04_Target-Action
来源:互联网 发布:二战三巨头知乎 编辑:程序博客网 时间:2024/05/21 18:34
//// MyButton.h// UI04_Target-Action//// Created by dllo on 15/8/3.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import <UIKit/UIKit.h>@interface MyButton : UIView//通过MyButton实现button的点击效果//1.通过自定义的方法,把目标和动作传到类的内部-(void)addTarget:(id)target Action:(SEL)action;//target:目标,button执行哪一个类的方法,对应的目标就是那个类的对象//action:动作,让button具体做什么事,执行的方法就是对应的动作//2.通过两条属性,把对应的目标和动作保存起来@property(nonatomic,assign)id target;@property(nonatomic,assign)SEL action;@end
//// MyButton.m// UI04_Target-Action//// Created by dllo on 15/8/3.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import "MyButton.h"@implementation MyButton-(void)addTarget:(id)target Action:(SEL)action{ //3.实现对应的自定义方法,并且让两个属性来保存对应的目标和动作 self.action=action; self.target=target;}//4.给Button一个触发的条件,重写触摸方法,只要一触碰touchBegan方法,就会让button执行相应点击方法-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{ //5.类把他的方法交给MyButton来完成 [self.target performSelector:self.action withObject:self];}@end
//// MainViewController.m// UI04_Target-Action//// Created by dllo on 15/8/3.// Copyright (c) 2015年 zhozhicheng. All rights reserved.//#import "MainViewController.h"#import "MyButton.h"@interface MainViewController ()@end@implementation MainViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. // 通过UIView来模拟一个Button点击 MyButton *myButton=[[MyButton alloc] initWithFrame:CGRectMake(100, 100, 150, 40)]; myButton.backgroundColor=[UIColor yellowColor]; myButton.layer.borderWidth=1; myButton.layer.cornerRadius=10; [self.view addSubview:myButton]; [myButton release]; //6.使用自定义的方法 [myButton addTarget:self Action:@selector(click:)];}-(void)click:(UIButton *)button{ NSLog(@"实现点击效果");}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}/*#pragma mark - Navigation// In a storyboard-based application, you will often want to do a little preparation before navigation- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender { // Get the new view controller using [segue destinationViewController]. // Pass the selected object to the new view controller.}*/@end
0 0
- iOS UI04_Target-Action
- UI04_Target-Action
- UI04_Target-Action
- iOS In Action (1)
- iOS Target-Action
- iOS Target-Action
- iOS target-action模式
- iOS Action Extension
- 【iOS开发】理解Action、IBACtion、Action Connection
- iOS 开发之Target-Action
- IOS--UI--LessonTarget/Action /Delegate
- ios添加pre和post build action
- iOS添加pre和post build action
- IOS开发 Target-Action方法的实现
- iOS 开发之Target-action模式
- iOS 开发之Target-action模式
- iOS设计模式之Target-Action
- iOS-target/action模式,delegate模式
- Obj-C内存管理(二)
- 黑马程序员--iOS--Objective-C基础 (内存管理三)
- 暑假集训第三周周六赛 STL D - 胜利大逃亡
- 在VS2010上配置SGI_STL
- XCode上设备或证书不能自动建和修改的问题解决
- iOS UI04_Target-Action
- POJ1465 Multiple BFS+同余判重
- 用objective-c开发的第一个mac程序(3)——动态库与静态库的问题
- 黑马程序员——Java 面型对象_封装
- 27 水池数目【dfs】
- solr学习
- zoj 2100 Seeding 【dfs】
- 【Linux】如何用shell编写斐波那契函数
- Linux&C open creat read write lseek 函数用法总结