Since ios class defines a binding method (where there is a pit)

As follows: direct transfer self is not feasible.

#import <UIKit/UIKit.h>

@interface XMPickDataView : UIView

+(void) onClick;

@end

@interface XMPickDataView()

@end

@implementation XMPickDataView

+(void) onClick{

	UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    [btn addTarget:self action:@selector(add) forControlEvents:UIControlEventTouchUpInside];	

}
-(void) add{
    NSLog(@"add:");
}

@end


Solution: At this time we need, when you call this method to initialize its own, you use another instance of the class does not work, because when this method to bind with other class, his class inside this method is not you. your way is in the class definition, you are forced to pass an inappropriate objects will complain, can not find method

#import <UIKit/UIKit.h>

@interface XMPickDataView : UIView

+(void) onClick;

@end

@interface XMPickDataView()

@end

@implementation XMPickDataView

+(void) onClick{
	//初始化一个自己
	XMPickDataView *custom = [[XMPickDataView alloc] initWithFrame:CGRectMake(0,-10,10,10)];
    [[UIApplication sharedApplication].keyWindow addSubview:custom];
	UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 200, 100)];
    [btn addTarget:custom action:@selector(add) forControlEvents:UIControlEventTouchUpInside];	

}
-(void) add{
    NSLog(@"add:");
}

@end


Published 12 original articles · won praise 5 · Views 5180

Guess you like

Origin blog.csdn.net/qq_41586150/article/details/104070340