UI - TargetAction模式

来源:互联网 发布:js执行按钮点击事件 编辑:程序博客网 时间:2024/04/30 08:10

<AppDelegate.m>

#import "AppDelegate.h"#import "RootViewController.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];    [self.window makeKeyAndVisible];        RootViewController *rootVC = [[RootViewController alloc ]init ];    self.window.rootViewController = rootVC;    [rootVC release];        return YES;}- (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


<RootViewController.h>

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


<RootViewController.m>

#import "RootViewController.h"#import "TouchView.h"@interface RootViewController ()@end@implementation RootViewController- (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.        //创建对象 touchView1    TouchView *touchView1 = [[TouchView alloc] initWithFrame:CGRectMake(50, 100, 220, 100)];    touchView1.backgroundColor = [UIColor redColor];    //添加点击事件    [touchView1 addTarget:self action:@selector(changeColor:)];      [self.view addSubview:touchView1];    [touchView1 release];        //touchView2    TouchView *touchView2 = [[TouchView alloc ]initWithFrame:CGRectMake(50, 250, 220, 100)];    touchView2.backgroundColor = [UIColor greenColor];    [touchView2 addTarget:self action:@selector(changePosition:)];    [self.view addSubview:touchView2];    [touchView2 release];        //touchView3    TouchView *touchView3 = [[TouchView alloc]initWithFrame:CGRectMake(50, 400, 220, 100)];    touchView3.backgroundColor = [UIColor yellowColor];    [touchView3 addTarget:self action:@selector(changeSize:)];    [self.view addSubview:touchView3];    [touchView3 release];        }-(void)changeColor:(TouchView *)view{    view.backgroundColor = [UIColor colorWithRed:arc4random()%256 / 255.0 green:arc4random()%256 / 255.0 blue:arc4random()%256 / 255.0 alpha:1];}-(void)changePosition:(TouchView *)view{    view.center = CGPointMake(arc4random()%200, arc4random()%200);}-(void)changeSize:(TouchView *)view{    view.bounds = CGRectMake(0, 0, arc4random()%200 + 10, arc4random()%150 + 10);    //以中心点为标准来改变大小}- (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

<TouchView.h>

#import <UIKit/UIKit.h>@interface TouchView : UIView{    id _target;//记录执行对象    SEL _action;//记录行为    SEL是对方法的一种包装。包装的SEL类型数据它对应相应的方法地址,找到方法地址就可以调用方法。在内存中每个类的方法都存储在类对象中,每个方法都有一个与之对应的SEL类型的数据,根据一个SEL数据就可以找到对应的方法地址,进而调用方法}-(void)addTarget:(id)target action:(SEL)action;@end


<TouchView.m>

#import "TouchView.h"@implementation TouchView- (id)initWithFrame:(CGRect)frame{    self = [super initWithFrame:frame];    if (self) {        // Initialization code    }    return self;}-(void)addTarget:(id)target action:(SEL)action;{    _target = target;//存储外界传入的对象    _action = action;//存储外界传来的行为}-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{    //让外界对象执行给定的方法    [_target performSelector:_action withObject:self];//self 是将本身作为参数传入进去,若不想传入参数则可以将 self 换为 nil    }/*// Only override drawRect: if you perform custom drawing.// An empty implementation adversely affects performance during animation.- (void)drawRect:(CGRect)rect{    // Drawing code}*/@end


0 0
原创粉丝点击