Target_Action

来源:互联网 发布:威廉叶芝知乎 编辑:程序博客网 时间:2024/05/22 09:05

#import "AppDelegate.h"

#import "MainViewController.h"

@interface AppDelegate ()


@end


@implementation AppDelegate


- (void)dealloc

{

    [_windowrelease];

    [superdealloc];


}



- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

    self.window = [[UIWindowalloc] initWithFrame:[[UIScreenmainScreen] bounds]];

    // Override point for customization after application launch.

    self.window.backgroundColor = [UIColorwhiteColor];

    [self.windowmakeKeyAndVisible];

    [_windowrelease];

    

    MainViewController *mainVC = [[MainViewControlleralloc] init];

    self.window.rootViewController = mainVC;

    [mainVCrelease];

    

    return YES;

}





#import "MainViewController.h"

#import "TouchView.h"

@interface MainViewController ()


@end


@implementation MainViewController


- (void)viewDidLoad {

    [superviewDidLoad];

    // Do any additional setup after loading the view.

    

    //自定义视图实现点击

   TouchView *touchV = [[TouchViewalloc] initWithFrame:CGRectMake(20,20, 335, 335)];

    touchV.backgroundColor = [UIColorredColor];

//    touchV.alpha = 0;

    [self.viewaddSubview:touchV];

    [touchVrelease];

    

    

    touchV.target =self;

    touchV.action =@selector(touchAction:);

    

    

    

}


- (void)touchAction:(TouchView *)view

{


    NSLog(@"touchView被点击了");


}


#import <UIKit/UIKit.h>


@interface TouchView :UIView



// 执行方法的对象

@property (nonatomic,assign)id target;


// target对象的一个方法

@property (nonatomic,assign)SEL action;


@end



#import "TouchView.h"


@implementation TouchView


- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event

{

   NSLog(@"点击");

//    CGFloat red = arc4random() % 256 / 255.0;

//    CGFloat green = arc4random() % 256 / 255.0;

//    CGFloat blue = arc4random() % 256 / 255.0;

//    self.backgroundColor = [UIColor colorWithRed:red green:green blue:blue alpha:1.0];

    

    // target 调用 action 方法

    // targetaction 具体是什么,TouchView本身不需要关心

    [self.targetperformSelector:self.actionwithObject:nil];


}

@end




0 0
原创粉丝点击