//

//  SetViewController.m

//  dfhx

//

//  Created by dfhx_iMac_001 on 16/4/5.

//  Copyright © 2016年 luoyun. All rights reserved.

//

#import "SetViewController.h"

@interface SetViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView * tableView;

@property (nonatomic, strong) NSArray * dataSource;

@property (nonatomic, copy) NSString * cacheSize;

@property (nonatomic, strong) UIView * navView;

@end

@implementation SetViewController

- (void)viewDidLoad {

[super viewDidLoad];

// Do any additional setup after loading the view.

[self tableView];

[self navView];

}

- (UIView *)navView

{

if (!_navView) {

_navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, 64)];

_navView.backgroundColor = RGBA(51, 51, 51, 1);

[self.view addSubview:_navView];

UIButton * btn = [UIButton buttonWithType:UIButtonTypeCustom];

[_navView addSubview:btn];

[btn mas_makeConstraints:^(MASConstraintMaker *make) {

make.left.mas_equalTo(_navView).offset(10);

make.size.mas_equalTo(CGSizeMake(40, 40));

make.topMargin.offset(20);

}];

[btn addTarget:self action:@selector(btnAction) forControlEvents:UIControlEventTouchUpInside];

[btn setImage:[UIImage imageNamed:@"backItemImg.png"] forState:UIControlStateNormal];

[btn setImageEdgeInsets:UIEdgeInsetsMake(15, 5, 8, 23)];

}

return _navView;

}

- (void)btnAction

{

AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;

[app.window setRootViewController:app.leftSlide];

}

// 设置返回导航栏

- (void)setNavigation

{

UIBarButtonItem *LeftButton = [[UIBarButtonItem  alloc] initWithTitle:nil style:UIBarButtonItemStylePlain target:self action:@selector(back)];

[LeftButton setImage:[UIImage imageNamed:@"backItemImg.png"]];

LeftButton.imageInsets = UIEdgeInsetsMake(20, 0, 15, 20);

LeftButton.tintColor = [UIColor whiteColor];

self.navigationItem.leftBarButtonItem = LeftButton;

}

- (void)back

{

[self.navigationController popViewControllerAnimated:YES];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

{

return 1;

}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath

{

static NSString * reuseID = @"cell";

UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:reuseID];

if (!cell) {

cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:reuseID];

}

cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;

cell.textLabel.text =@"清除缓存";

cell.detailTextLabel.text = self.cacheSize;

cell.selectionStyle = UITableViewCellSeparatorStyleSingleLine;

[self myClearCacheAction:NO];

return cell;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath

{

if ([self.cacheSize isEqualToString:@"0.00MB"]) {

[self alertView:@"当前无缓存!" cancel:@""];

}

else

{

[self alertView:@"确认清除缓存?" cancel:@"取消"];

}

}

- (void)alertView:(NSString *)alertTitle cancel:(NSString *)cancelTitle

{

UIAlertController *alertController = [UIAlertController alertControllerWithTitle:alertTitle message:@"" preferredStyle:UIAlertControllerStyleAlert];

UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:cancelTitle style:UIAlertActionStyleCancel handler:nil];

UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"好的" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

if ([alertTitle isEqualToString:@"确认清除缓存?"]) {

//清除缓存

[self myClearCacheAction:YES];

}

}];

if ([cancelTitle isEqualToString:@""]) {

[alertController addAction:okAction];

}

else

{

[alertController addAction:cancelAction];

[alertController addAction:okAction];

}

[self presentViewController:alertController animated:YES completion:nil];

}

//清理缓存

-(void)myClearCacheAction:(BOOL)isClear

{

if (isClear == YES) {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachePath];

for (NSString *p in files) {

NSError *error;

NSString *path = [cachePath stringByAppendingPathComponent:p];

if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {

//清理缓存文件

[[NSFileManager defaultManager] removeItemAtPath:path error:&error];

}

}

[self performSelectorOnMainThread:@selector(clearCacheSuccess) withObject:nil waitUntilDone:YES];

});

}

else

{

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^{

NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];

CGFloat fileSize = [self folderSizeAtPath:cachePath];

dispatch_async(dispatch_get_main_queue(), ^{

NSString * t = [NSString stringWithFormat:@"%.2fMB",fileSize];

self.cacheSize = t;

NSIndexPath *indexPath=[NSIndexPath indexPathForRow:0 inSection:0];

[self.tableView reloadRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath,nil] withRowAnimation:UITableViewRowAnimationNone];

});

});

}

}

-(void)clearCacheSuccess

{

NSLog(@"缓存已清除!");

}

//计算目录下所有文件大小

- (CGFloat)folderSizeAtPath:(NSString *)folderPath

{

NSFileManager *manager = [NSFileManager defaultManager];

if (![manager fileExistsAtPath:folderPath]) {

return 0;

}

NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];

NSString *fileName = nil;

long long folderSize = 0;

while ((fileName = [childFilesEnumerator nextObject]) != nil) {

NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

folderSize += [self fileSizeAtPath:fileAbsolutePath];

}

return folderSize/(1024.0*1024.0);

}

//计算文件大小

- (long long)fileSizeAtPath:(NSString *)filePath

{

NSFileManager* manager = [NSFileManager defaultManager];

if ([manager fileExistsAtPath:filePath]){

return [[manager attributesOfItemAtPath:filePath error:nil] fileSize];

}

return 0;

}

// 懒加载tableview

- (UITableView *)tableView

{

if (!_tableView) {

_tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.width, self.view.height) style:UITableViewStylePlain];

_tableView.dataSource = self;

_tableView.delegate  = self;

_tableView.backgroundColor = [UIColor whiteColor];

_tableView.showsHorizontalScrollIndicator = NO;

_tableView.showsVerticalScrollIndicator = NO;

_tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

_tableView.scrollEnabled = NO;

[self.view addSubview:_tableView];

}

return _tableView;

}

#pragma mark ------当页面出现和消失的时候--------

- (void)viewWillAppear:(BOOL)animated

{

[super viewWillAppear:animated];

self.tabBarController.tabBar.hidden = YES;

}

- (void)viewWillDisappear:(BOOL)animated

{

[super viewWillDisappear:animated];

self.navigationController.navigationBarHidden = NO;

self.tabBarController.tabBar.hidden = NO;

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

// Dispose of any resources that can be recreated.

}

/*

#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

// Get the new view controller using [segue destinationViewController].

// Pass the selected object to the new view controller.

}

*/

@end

iOS开发 -李洪强-清除缓存的更多相关文章

  1. 李洪强iOS开发之数据存储

    李洪强iOS开发之数据存储 iOS应用数据存储的常用方式 1.lXML属性列表(plist)归档 2.lPreference(偏好设置) 3.lNSKeyedArchiver归档(NSCoding) ...

  2. 李洪强漫谈iOS开发[C语言-002]-开发概述程序的本质与简单执行过程

    李洪强iOS开发之应用程序的本质与简单执行过程 什么叫程序? 就是一段执行指令 程序的两个状态: 保存状态(保存到硬盘上)   运行状态(由CPU执行) 代码可以执行吗? CPU(中央处理器-> ...

  3. 李洪强iOS开发之性能优化技巧

    李洪强iOS开发之性能优化技巧 通过静态 Analyze 工具,以及运行时 Profile 工具分析性能瓶颈,并进行性能优化.结合本人在开发中遇到的问题,可以从以下几个方面进行性能优化. 一.view ...

  4. 李洪强漫谈iOS开发[C语言-043]-判断较早日期

    李洪强漫谈iOS开发[C语言-043]-判断较早日期

  5. 李洪强iOS开发之添加手势

    李洪强iOS开发之添加手势 02 - 添加手势

  6. 李洪强iOS开发之- 实现简单的弹窗

     李洪强iOS开发之- 实现简单的弹窗 实现的效果:  112222222222223333333333333333

  7. 李洪强iOS开发之后使用XIB实现横向滚动的UIScrollView

    李洪强iOS开发之后使用XIB实现横向滚动的UIScrollView 11111222

  8. 李洪强iOS开发之苹果使用预览截图

    李洪强iOS开发之苹果使用预览截图 01 在预览的图片中选中你要截得区域  02 - command + C   03 - Command + N 04 - Command + S (保存)

  9. 李洪强iOS开发之通知的使用

    李洪强iOS开发之通知的使用 01 - 在A中发送通知 02 - 在B中监听通知 03 - 在B中通知出发的方法 04 - 在B控制器viewDidLoad调用通知

随机推荐

  1. JS isNaN()函数

    1.作用 isNaN() 函数用于检查其参数是否是非数字值. 2.JS <script> document.write(isNaN(123)); document.write(isNaN( ...

  2. 关于V1.6.0版本的项目总结

    10月28号,我们开了1.6项目的总结大会,包括Ting总在内,前端.后台.运维都来参加总结大会了.虽然目标基本达成,但同时也暴露出很多问题,我们希望各方能够坐下来总结一下自己或者各个相关团队的功过是 ...

  3. Windows 7下Maven3.0.3的安装

    1.去Maven官网下载zip的maven3.0.3压缩包 2.将安装包解压到某目录,我在这里解压到D:\Program Files (x86)\apache-maven-3.0.3 3.设置系统变量 ...

  4. 公司机器(线上机器)启动ftp任务的命令

    这个命令: /usr/local/proftp/sbin/proftpd 注意要在root账户,并且kill掉原来的同名进程.

  5. stylus使用文档总结:内置方法+参数+条件+迭代+导入+继承

    一.内置方法 返回各种颜色的比重(如red(color)等) 颜色函数是CSS预处里器中内置的颜色函数功能,这些功能可以对颜色值进行处理,例如颜色的变亮.变暗.渐变颜色等处理十分的方便. lighte ...

  6. java程序计算数独游戏

    兴趣来了,写了个简单的数独游戏计算程序,未做算法优化. 通过文件来输入一个二维数组,9行,每行9个数组,数独游戏中需要填空的地方用0来表示.结果也是打印二维数组. import java.io.Fil ...

  7. Tomcat启动报错:java.net.BindException: Cannot assign requested address: JVM_Bind

    Tomcat启动报错:java.net.BindException: Cannot assign requested address: JVM_Bind Tomcat Cannot assign re ...

  8. Activity生命周期以及启动模式对生命周期的影响(二)

    前面一篇文章概述了Android四大组件之一的Activity生命周期方法的调用先后顺序,但对于非标准启动模式下Activity被多次调用时的一些生命周期方法并未详细阐述,现在针对该情况着重记录. 现 ...

  9. Building An Effective Marketing Plan

    “New ideas are a dime a dozen,” observes Arthur R. Kydd, “and so are new products and new technologi ...

  10. E492: Not an editor command: ^M

    在windows下拷贝vimrc到Linux,运行vim命令后,出现错误 vim E492: Not an editor command: ^M 原因: linux的文件换行符为\n,但windows ...