如何删除UILabel的子类本身

时间:2018-03-27 13:46:17

标签: objective-c uiviewcontroller uilabel

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

有什么想法吗?

1 个答案:

答案 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];