5.通过storyboard创建视图控制器
MoreViewController
*fifthCtrl = [[
UIStoryboard
storyboardWithName
:
@"MoreViewController"
bundle
:
nil
]
instantiateViewControllerWithIdentifier
:
@"MoreViewController"
];
6.清除缓存
(1).计算缓存大小
/**
计算缓存大小
*/
- ( void )_cacheSize { /** 获取缓存文件夹路径 */ NSString *cachePath = [ NSHomeDirectory () stringByAppendingPathComponent : @"/Library/Caches/ImageCache" ]; /** 创建文件管理者 */ NSFileManager *fileMG = [ NSFileManager defaultManager ]; /** 获取路径下面的所有文件名称 */ NSArray *cacheNames = [fileMG subpathsAtPath :cachePath]; /** 获取每个缓存文件的大小 */ long long sum = 0 ; /** 遍历数组拿到文件名计算大小 */ for ( NSString *name in cacheNames) { /** 文件路径 */ NSString *imgCachePath = [cachePath stringByAppendingPathComponent :name]; /** 获取文件属性 */ NSDictionary *imgDict = [fileMG attributesOfItemAtPath :imgCachePath error : nil ]; /** 获取文件大小 */ long long size = [imgDict fileSize ]; /** 记录文件大小 */ sum += size; } self . cache . text = [ NSString stringWithFormat : @"%.1fM" , sum/ 1024.0 / 1024.0 ];
}
|
(2).选中清除缓存执行操作
/**
选中单元格调用方法
*/
- ( void )tableView:( UITableView *)tableView didSelectRowAtIndexPath:( NSIndexPath *)indexPath { /** 取消单元格选中 带动画 */ [tableView deselectRowAtIndexPath :indexPath animated : YES ]; if (indexPath. row == 0 ) { UIAlertView *alert = [[ UIAlertView alloc ] initWithTitle : @" 提示 " message : @" 清除缓存 " delegate : self cancelButtonTitle : @" 取消 " otherButtonTitles : @" 确定 " , nil ]; [alert show ]; }
}
/**遵守
UIAlertViewDelegate
协议 并实现点击事件
*/
- (
void
)alertView:(
UIAlertView
*)alertView clickedButtonAtIndex:(
NSInteger
)buttonIndex
{ if (buttonIndex == 1 ) { /** 获取缓存文件夹路径 */ NSString *cachePath = [ NSHomeDirectory () stringByAppendingPathComponent : @"/Library/Caches/ImageCache" ]; NSFileManager *fileMG = [ NSFileManager defaultManager ]; /** 删除缓存文件夹 */ [fileMG removeItemAtPath :cachePath error : nil ]; /** 创建文件夹 */
[fileMG createDirectoryAtPath:cachePath withIntermediateDirectories:YESattributes:nilerror:nil];
/**实时刷新缓存显示
*/
[
self
_cacheSize
];
} } |
(3).设置视图显示时实时刷新缓存
/**
视图将要显示时刷新缓存
*/
- ( void )viewWillAppear:( BOOL )animated { [ super viewWillAppear :animated];
/**实时刷新缓存显示
*/
[
self
_cacheSize
];
}
|
// 执行清除缓存
NSFileManager *mgr = [NSFileManager defaultManager];
NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];
[mgr removeItemAtPath:cachePath error:nil];
// 计算缓存文件夹的大小
NSArray *subpaths = [mgr subpathsAtPath:cachePath];
long long totalSize = 0;
for (NSString *subpath in subpaths) {
NSString *fullpath = [cachePath stringByAppendingPathComponent:subpath];
BOOL dir = NO;
[mgr fileExistsAtPath:fullpath isDirectory:&dir];
if (dir == NO) {// 文件
totalSize += [[mgr attributesOfItemAtPath:fullpath error:nil][NSFileSize] longLongValue];
}
}