为什么自定义 class 无法成功调用自己的 class 方法? iOS
why the custom class could not successfully call its own class method? iOS
代码如下:
在myViewController.m文件中:
A.This一个可行(实例方法)
-(UIButton *)createButton:(SEL)action addedto:(UIView *)fatherView
{
UIButton *btn = [[UIButton alloc] initWithFrame:frame];
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[fatherView addSubview:btn];
return btn;
}
-(void)startBtnDidClicked:(UIButton *)startBtn
{
NSLog(@"start!");
}
-(void)viewDidLoad
{
self.startBtn = [self createButton:@selector(startBtnDidClicked:) addedto:self.parentViewController.view ];
}
B.But 这个 无法工作(class 方法):[错误:当触发 "startBtnDidClicked:" 时,它显示如下错误:"无法识别的选择器发送至
xxxx"]
+ (UIButton *)createButton:(SEL)action addedto:(UIView *)fatherView
{
UIButton *btn = [[UIButton alloc] initWithFrame:frame];
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[fatherView addSubview:btn];
return btn;
}
-(void)startBtnDidClicked:(UIButton *)startBtn
{
NSLog(@"start!");
}
-(void)viewDidLoad
{
self.startBtn = [[self class] createButton:@selector(startBtnDidClicked:) addedto:self.parentViewController.view ];
}
在 class 方法 createButton:added:
中(在您的第二段代码中),self
是对 class 的引用,而不是 [= 的实例20=]。所以你的按钮正在寻找一个名为 startBtnDidClicked:
的 class 方法,而不是一个名为 startBtnDidClicked:
.
的实例方法
如果要使用class方法创建按钮,则需要为目标添加另一个参数而不是假设self
。
+ (UIButton *)createButton:(id)target action:(SEL)action addedto:(UIView *)fatherView
{
UIButton *btn = [[UIButton alloc] initWithFrame:frame];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[fatherView addSubview:btn];
return btn;
}
-(void)startBtnDidClicked:(UIButton *)startBtn
{
NSLog(@"start!");
}
-(void)viewDidLoad
{
self.startBtn = [[self class] createButton:self action:@selector(startBtnDidClicked:) addedto:self.parentViewController.view ];
}
代码如下:
在myViewController.m文件中:
A.This一个可行(实例方法)
-(UIButton *)createButton:(SEL)action addedto:(UIView *)fatherView
{
UIButton *btn = [[UIButton alloc] initWithFrame:frame];
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[fatherView addSubview:btn];
return btn;
}
-(void)startBtnDidClicked:(UIButton *)startBtn
{
NSLog(@"start!");
}
-(void)viewDidLoad
{
self.startBtn = [self createButton:@selector(startBtnDidClicked:) addedto:self.parentViewController.view ];
}
B.But 这个 无法工作(class 方法):[错误:当触发 "startBtnDidClicked:" 时,它显示如下错误:"无法识别的选择器发送至 xxxx"]
+ (UIButton *)createButton:(SEL)action addedto:(UIView *)fatherView
{
UIButton *btn = [[UIButton alloc] initWithFrame:frame];
[btn addTarget:self action:action forControlEvents:UIControlEventTouchUpInside];
[fatherView addSubview:btn];
return btn;
}
-(void)startBtnDidClicked:(UIButton *)startBtn
{
NSLog(@"start!");
}
-(void)viewDidLoad
{
self.startBtn = [[self class] createButton:@selector(startBtnDidClicked:) addedto:self.parentViewController.view ];
}
在 class 方法 createButton:added:
中(在您的第二段代码中),self
是对 class 的引用,而不是 [= 的实例20=]。所以你的按钮正在寻找一个名为 startBtnDidClicked:
的 class 方法,而不是一个名为 startBtnDidClicked:
.
如果要使用class方法创建按钮,则需要为目标添加另一个参数而不是假设self
。
+ (UIButton *)createButton:(id)target action:(SEL)action addedto:(UIView *)fatherView
{
UIButton *btn = [[UIButton alloc] initWithFrame:frame];
[btn addTarget:target action:action forControlEvents:UIControlEventTouchUpInside];
[fatherView addSubview:btn];
return btn;
}
-(void)startBtnDidClicked:(UIButton *)startBtn
{
NSLog(@"start!");
}
-(void)viewDidLoad
{
self.startBtn = [[self class] createButton:self action:@selector(startBtnDidClicked:) addedto:self.parentViewController.view ];
}