UILabel
中名为NewLabel
的{{1}}的子类,位于NewLabel.m
+ (NewLabel*)addLabelIntoView:(UIView*)view
{
NewLabel *label = [[NewLabel alloc] init];
CGSize size = CGSizeMake(120.0f, 40.0f);
CGPoint point = CGPointMake(view.bounds.size.width / 2, view.bounds.size.height / 2);
label.frame = CGRectMake(0, 0, size.width, size.height);
label.center = point;
[view addSubview:label];
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(removeLabelFromView) userInfo:nil repeats:NO];
return label;
}
- (void)removeLabelFromView {
[self removeFromSuperview];
}
在UIViewController.m
中,我添加了这个以显示NewLabel,并自行关闭
[NewLabel addLabelIntoView:self.view]
但它总是崩溃,这是崩溃信息
+[NewLabel removeLabelFromView]: unrecognized selector sent to class 0x103d40738
有什么想法吗?
答案 0 :(得分:0)
在类方法中self
是类。而不是+[NewLabel removeLabelFromView]
你可能想做-[NewLabel removeLabelFromView]
。变化
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(removeLabelFromView) userInfo:nil repeats:NO];
到
[NSTimer scheduledTimerWithTimeInterval:1.0f target:label selector:@selector(removeLabelFromView) userInfo:nil repeats:NO];