target-action设计模式--主要为Button的方法重写

新建两个类MainViewController/ButtonView

ButtonView.h

#import <UIKit/UIKit.h>
@interface ButtonView : UIView
//实现target-action设计模式
//点击的时候让谁去执行方法
@property (nonatomic , assign) id target;
//要执行的方法
@property (nonatomic , assign) SEL action;
//模拟一个UIButton的一个方法
- (void)addTarget:(id)target action:(SEL)action;
@end

ButtonView.m

#import "ButtonView.h"
@implementation ButtonView
- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}
//建立一个view,让这个view的作用和UIButton类似 作用;点击能够响应事件
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    //点击时候就有反应
    
    
}
- (void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event
{
    NSLog(@"摸完了,可口可乐,冒泡");
    
    //每当view被点击的时候,就让target执行action方法
    //target\action设计模式核心
    [_target performSelectorInBackground:self.action withObject:self];
}
//利用方法给view设置对象和对象要执行的方法
- (void)addTarget:(id)target action:(SEL)action
{
    self.target = target;
    self.action = action;
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/
@end

MainViewController.h

#import <UIKit/UIKit.h>
@interface MainViewController : UIViewController
@end

MainViewController.m

#import "MainViewController.h"
#import "ButtonView.h"
@interface MainViewController ()
@end
@implementation MainViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    ButtonView *button = [[ButtonView alloc] initWithFrame:CGRectMake(120, 50, 80, 30)];
    button.backgroundColor = [UIColor purpleColor];
    button.alpha = 0.3;
    button.layer.cornerRadius = 10;
    //给view添加一个触发方法
    [button addTarget:self action:@selector(buttonClicked:)];
    
    [self.view addSubview:button];
    [button release];
    
    //UIImageView  
    //是一个显示图片的view
    UIImageView *imageView = [[UIImageView alloc] initWithFrame:CGRectMake(100, 100, 50, 50)];
    [self.view addSubview:imageView];
    [imageView release];
    //给imageView设置一个显示的图片
    UIImage *image = [UIImage imageNamed:@"1.png"];
    imageView.image = image;
    //如果在imageView 上添加按钮等视图,需要打开imageView的用户交互属性
    [imageView setUserInteractionEnabled:YES];
    
    UIImageView *imageView1 = [[UIImageView alloc] initWithFrame:CGRectMake(160, 100, 50, 50)];
    [self.view addSubview:imageView1];
    [imageView1 release];
    UIImage *image1 = [UIImage imageNamed:@"2.png"];
    imageView1.image = image1;
    
}
- (void)buttonClicked:(ButtonView *)button
{
    NSLog(@"button触发后的方法");
}
- (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

AppDelegate.h

#import <UIKit/UIKit.h>
@interface AppDelegate : UIResponder <UIApplicationDelegate>
@property (strong, nonatomic) UIWindow *window;
@end

AppDelegate.m

#import "AppDelegate.h"
#import "MainViewController.h"
@implementation AppDelegate
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    //初始化视图控制器
    MainViewController *mainVC = [[MainViewController alloc] init];
    self.window.rootViewController = mainVC;
    [mainVC release];
    
    
    [self.window makeKeyAndVisible];
    
    
    [_window release];
    return YES;
}
- (void)dealloc
{
    [_window release];
    [super dealloc];
}
- (void)applicationWillResignActive:(UIApplication *)application
{
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}
- (void)applicationDidEnterBackground:(UIApplication *)application
{
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. 
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}
- (void)applicationWillEnterForeground:(UIApplication *)application
{
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}
- (void)applicationDidBecomeActive:(UIApplication *)application
{
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}
- (void)applicationWillTerminate:(UIApplication *)application
{
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}
@end
时间: 2024-08-09 02:28:05

target-action设计模式--主要为Button的方法重写的相关文章

UI开发----target/action设计模式和代理设计模式以及手势识别器

//  Created By 郭仔  2015年04月16日21:50:33 眼睛有点痛,视力也在急速下降,心灵之窗,注意保护! ================================================ 耦合是衡量?一个程序写的好坏的标准之?一, 耦合是衡量模块与模块之间关联程度的指标 "?高内聚,低耦合"是?面向对象编程的核⼼心思想: ================ 设计模式思想很重要的 ================== target/action设计模

target - action设计模式的思想

不同的实例点击效果不同:点击改变自身颜色,点击改变父视图颜色,点击修改视图位置.以上效果可由target - action设计模式实现. - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. self.view.backgroundColor = [UIColor yellowColor]; CustomView *greenView = [[CustomView

UI 04 target...action 设计模式

"高内聚,低耦合"是面向对象编程的核心思想. 使用 target-action 实现解耦. 需要目标去执行一个动作的地方. 例如, 定义一个继承于UIView 的MyButton 类, 让他能够有Button的点击方法. 代码如下: Mybutton.h: #import <UIKit/UIKit.h> @interface Mybutton : UIView //1.写一个自定义方法,把目标和对应动作传过来. - (void)addNewTarget:(id)target

target/action 设计模式

Target-Action模式是ObjC里非常常见的对象之间方法调用的方式,不过ObjC把方法调用叫做Send Message. 一帮情况在和UI打交道时候处理各种GUI上的事件会利用到这种模式.相对应的.NET上的处理模式就是delegate/event了. 不过,Target-Action拜C语言所赐,更是灵活很多,编译期没有任何检查,都是运行时的绑定. 看代码 首先我们创建一个继承UIView的类 1 #import <UIKit/UIKit.h> 2 3 @interface Touc

UI基础:target...action设计模式,手势识别器.UIimageview

使用target..action和delegate设计模式可以实现解耦.使代码更加优化. 手势识别器: 手势识别器:是对触摸事件做了封装,无需自己去判断某个手势是否触发,手势识别器本身起到了识别作用,所在我们可以把重心放在识别之后该去做什么操作上面.很方便. 手势识别器是iOS中比较抽象的一个类,用于识别一个手势,所谓的手势:有规律的触摸. 手势识别器有7个子类: 分别是:轻怕手势,轻移手势,清扫手势,缩放手势,旋转手势,长按手势,以及屏幕边缘平移手势. 一旦指定的手势别识别了,就可以执行自定义

target/action 设计模式简单使用

Target-Action模式是ObjC里非常常见的对象之间方法调用的方式,不过ObjC把方法调用叫做Send Message. 一帮情况在和UI打交道时候处理各种GUI上的事件会利用到这种模式.相对应的.NET上的处理模式就是delegate/event了. 不过,Target-Action拜C语言所赐,更是灵活很多,编译期没有任何检查,都是运行时的绑定. 代码演示: 1.创建一个继承UIView的类 #import <UIKit/UIKit.h> @interface TouchView

UI-target...action设计模式,手势识别器.UIimageview

target-action设计模式 iOS设计模式之Target-Action主要是为了降低代码的耦合性.顾名思义      Target-Action模式就是指  目标-动作模式,它贯穿于iOS开发始终. 提到Target-Action,先说2个词     “高内聚,低耦合”      这主要是评价一个软件的好坏 它评判软件的好坏主要靠模板之间内聚是否高,模块间耦合度是否低. 其实Target-action模式很简单,就是当某个事件发生时,调用那个对象中的那个方法.比如:点击按钮时,调用Con

进击的UI---------------target/action设计模式&amp;Delegate&amp;手势识别

1.target/action设计模式: AppDelegate.m RootViewController.m ClickView.h ClickView.m ColorView.h ColorView.m RootView.h RootView.m ButtonView.h ButtonView.m 2.delegate设计模式(代理): AppDelegate.m RootViewController.m RootView.h: RootView.m: colorView.h colorVi

自定义target - action 方法

HomeViewController.m #import "HomeViewController.h" #import "TouchViewPro.h" @interface HomeViewController (){ TouchViewPro *touchViewPro; } @end @implementation HomeViewController - (void)viewDidLoad { [super viewDidLoad]; touchViewPr