【代码笔记】iOS-removeFromSuper

代码:

RootViewController.m

复制代码
- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.title=@"removeFromSuperView";
    
    
    UILabel *tryLabel=[[UILabel alloc]initWithFrame:CGRectMake(50, 150, 200, 50)];
    tryLabel.backgroundColor=[UIColor redColor];
    [self.view addSubview:tryLabel];
    
}
//当点击任意处时,把UILabel去掉
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self removeFromView];
}
-(void)removeFromView
{
    //点击后删除之前的PickerView
    for (UIView *view in self.view.subviews) {
        if ([view isKindOfClass:[UILabel class]]) {
            [view removeFromSuperview];
        }
        
    }

}
复制代码
原文地址:https://www.cnblogs.com/yang-guang-girl/p/6991253.html