学习笔记

#import "ViewController.h"
#import "SecondViewController.h"
@interface ViewController ()
{
    UIButton *_testBtn1;
    UIButton *_testBtn2;
    int i ;
    UIImageView *_image;
    
    UIView *_tipView;
    
    
    

}
@end

@implementation ViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    _testBtn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    _testBtn2.frame = CGRectMake(160, 160, 60, 40);
    _testBtn2.backgroundColor = [UIColor blackColor];
    [_testBtn2 addTarget:self action:@selector(touch:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:_testBtn2];
}

- (void)touch:(id)sender{
    [self viewShow];
    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        [self writeToFile];
    });
}


- (void)viewShow{
    _tipView = [[UIView alloc]initWithFrame:CGRectMake(160-30, 160+100, 100, 100)];
    _tipView.backgroundColor = [UIColor redColor];
    _tipView.alpha = 0.5;
    [self.view addSubview:_tipView];
}

#pragma mark ------- 文件的写入

- (void)writeToFile{
    NSString *fileString = @"http://ezoon.cn/Data/Notice/Attach/9974ad21_973e_45e9_8b33_4f3f03484574.docx";
    NSData *fileData = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileString]];
    NSArray *fileNameArr = [fileString componentsSeparatedByString:@"/"];
    NSString *fileName = [fileNameArr lastObject];
    
    NSString *docDirPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
    NSString *fileDirectory = [NSString stringWithFormat:@"%@/file",docDirPath];
    
    NSString *filePath = [NSString stringWithFormat:@"%@/%@",fileDirectory,fileName];
    NSFileManager *manager = [NSFileManager defaultManager];
    BOOL fileExist = [manager fileExistsAtPath:filePath];
    if (fileExist) {
        [manager removeItemAtPath:filePath error:nil];
    }
    [manager createDirectoryAtPath:fileDirectory withIntermediateDirectories:YES attributes:nil error:nil];
    BOOL writeSuccess = [fileData writeToFile:filePath atomically:YES];
    dispatch_async(dispatch_get_main_queue(), ^{
        if (writeSuccess) {
            NSLog(@"success>>>>>>>>>");
            SecondViewController *svc = [[SecondViewController alloc]init];
            [self.navigationController pushViewController:svc animated:YES];
        }else{
            NSLog(@"fail 。。。。。");
        }
    });
}

/*
之所以要在线程里写入文件,是因为原本以为程序是顺序执行 即点击btn执行 viewshow  等一会(文件写入)跳到secondView  但实际上却是  点击btn后过了一会(文件写入)viewshow才执行 并且直接进入secondview  
 于是 把"文件写入"放到了线程里才是预期效果
 */



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值