iOS一行代码移除子视图,或者layer。iOS获取近八天日期数组

来源:互联网 发布:linux分屏显示命令 编辑:程序博客网 时间:2024/06/05 20:38

项目中大家可能都遇到过 需要删除某一个View的所有子View

但是,大家会发现iOS的View提供了一个 removeFromSuperview 的方法 可以将View从父级View上删除

但是,如何删除View的所有子View 好像没有一个更好的办法。

有一个方法是:通过 view.subviews 取到所有子View的数组 然后通过循环 removeFromSuperview 删除

今天我们提供一个更为方便的方法

数组的方法   makeObjectsPerformSelector

比如说我们要删除当前View的所有子View 一行代码即可搞定:
[self.view.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];  
同样的想法,当删除view的layer上面所有的layer层的时候,也可以
[self.view.layer.sublayers makeObjectsPerformSelector:@selector(removeFromSuperlayer)];


获取最近的八天的日期,
p.p1 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #008f00; background-color: #fdfff9}p.p2 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #000000; background-color: #fdfff9}p.p3 {margin: 0.0px 0.0px 0.0px 0.0px; font: 11.0px Menlo; color: #3495af; background-color: #fdfff9}span.s1 {color: #3495af}span.s2 {color: #000000}span.s3 {color: #0433ff}span.s4 {color: #b4261a}span.s5 {color: #008f00}

//获取最近八天时间 数组

- (NSMutableArray *)saleTotalX{

    NSMutableArray *eightArr = [[NSMutableArray alloc] init];

    for (int i = 0; i < 8; i ++) {

        //从现在开始的24小时

        NSTimeInterval secondsPerDay = -i * 24*60*60;

        NSDate *curDate = [NSDate dateWithTimeIntervalSinceNow:secondsPerDay];

        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];

        [dateFormatter setDateFormat:@"MM-dd"];

        NSString *dateStr = [dateFormatter stringFromDate:curDate];//几月几号

        [eightArr addObject:dateStr];

    }

    return eightArr;

}