UI阶段的 Target/Action设计模式
来源:互联网 发布:戴尔外星人灯光软件 编辑:程序博客网 时间:2024/06/09 14:04
问题:我怎么实现通过点击,一个UIImageView的对象,实现相应的操作,这里以更换图片为例
思路一:仿照UIButton的target/action设计模式
(MFC 手动管理内存)
创建一个类继承UIImageView,来实现该功能
ButtonImageView.h
#import <UIKit/UIKit.h>@interface ButtonImageView : UIImageView@property(nonatomic,strong)id target;@property(nonatomic,assign)SEL action;- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action;@end
ButtonImageView.m
#import "ButtonImageView.h"@implementation ButtonImageView- (instancetype)initWithFrame:(CGRect)frame target:(id)target action:(SEL)action{ // 完整的初始化方法 self = [super initWithFrame:frame]; if (self) { self.target = target; self.action = action; } return self;}// 由于你需要点击,之后有相关的操作// 重写touches的相关方法- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{}- (void)touchesCancelled:(NSSet *)touches withEvent:(UIEvent *)event{}- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event{ // 更换图片,只需要点击一下 // 这里需要用该类的一个对象,去调用该类的方法 [self.target performSelector:self.action withObject:self];}- (void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event{}
创建一个根试图控制器:创建一个根视图控制器对象,赋值给window的跟视图属性
#import “AppDelegate.m”
#import "AppDelegate.h"#import "RootViewController.h"@interface AppDelegate ()@end@implementation AppDelegate- (void)dealloc{ [_window release]; [super dealloc];}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]autorelease ]; // Override point for customization after application launch. self.window.backgroundColor = [UIColor whiteColor]; [self.window makeKeyAndVisible]; // 设置根视图控制器 RootViewController *rootVC = [[RootViewController alloc]init]; self.window.rootViewController = rootVC; [rootVC release]; return YES;}@end
RootViewController.m
#import "RootViewController.h"#import "ButtonImageView.h"@interface RootViewController ()@end@implementation RootViewController- (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. UIImage *img = [UIImage imageNamed:@"5.jpg"]; ButtonImageView *BtnImgView = [[ButtonImageView alloc]initWithFrame:[UIScreen mainScreen].bounds target:self action:@selector(BtnImgViewClick:)]; BtnImgView.image = img; BtnImgView.userInteractionEnabled = YES; [self.view addSubview:BtnImgView]; [BtnImgView release];}- (void)BtnImgViewClick:(ButtonImageView *)btnImgView{ // 更换图片 UIImage *img1 = [UIImage imageNamed:@"6.jpeg"]; btnImgView.image = img1;}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}
**小结:
这个模式设计出来的类的对象,就像一个button一样,当你点击该对象后,将返回该类的touchesEnd方法,将你初始化出来的一个对象的target调用该对象的action方法,其实还可以携带参数(withObject:…).参数需不需要,根据你的action方法决定.这样做就是为了降低类和类之间的耦合,从而实现封装和提高代码的复用性功能.**
0 0
- UI阶段的 Target/Action设计模式
- UI target/action设计模式
- UI - Target-Action设计模式
- UI 04 target...action 设计模式
- UI-Target-action模式
- Target-Action 设计模式
- target/action设计模式
- Target-Action 设计模式
- target/action设计模式
- target/action 设计模式
- target - action 设计模式
- Target/Action 设计模式
- target - action设计模式的思想
- UIday05_zy01:target/action 设计模式
- 13.target/action设计模式
- UI开发----target/action设计模式和代理设计模式以及手势识别器
- iOS 对于Target-Action设计模式的理解
- iOS设计模式之Target-Action
- ant简介
- HTTP响应头信息和请求头信息详解
- Bootstrap边学边记
- 南大软院大神养成计划
- ACM常用模板——伸展树
- UI阶段的 Target/Action设计模式
- Python文件I/O
- 【黑马程序员】多线程
- mysql和oracle的语法上的差异
- poj1502
- 抽象工厂模式简单实例
- pyspark 【未完待续】
- 笔记八(圆周运动)
- Navicat for Mysql的操作使用