iOS 开发中一些常用到的代码/方法总结

2017-04-21 11:39:09来源:CSDN作者:sp_wei人点击

iOS 开发中一些常用到的代码/方法总结1.清除缓存
dispatch_async(                               dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)                               , ^{                                                                      NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) objectAtIndex:0];                                   NSArray *files = [[NSFileManager defaultManager] subpathsAtPath:cachPath];                                                                      for (NSString *p in files) {                                       NSError *error;                                       NSString *path = [cachPath stringByAppendingPathComponent:p];                                       if ([[NSFileManager defaultManager] fileExistsAtPath:path]) {                                           [[NSFileManager defaultManager] removeItemAtPath:path error:&error];                                       }                                   }                                   [self performSelectorOnMainThread:@selector(clearCacheSuccess)                                                          withObject:nil waitUntilDone:YES];});

//清除缓存成功后-(void)clearCacheSuccess{}
2.全局网络实时检测

利用Reachability第三方库,在AppDelegate里- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    // Override point for customization after application launch.    //全局实时检测网络是否连接    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(NetWorkChanged:) name:kReachabilityChangedNotification object:nil];    Reachability *internetReach = [Reachability reachabilityForInternetConnection];    [internetReach startNotifier];    NetworkStatus netStates = [internetReach currentReachabilityStatus];    if (netStates) {        // 网络正常    }        return YES;}-(void) NetWorkChanged :(NSNotification *)note{    Reachability* curReach = [note object];    NSParameterAssert([curReach isKindOfClass: [Reachability class]]);    NetworkStatus status = [curReach currentReachabilityStatus];    if (status == NotReachable) {        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"没有网络连接"delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];        [alert show];    }}
3.创建UItableview

//去除多余的分割线    _tableView.tableFooterView = [[UIView alloc] initWithFrame:CGRectZero];

4.添加手势,关闭键盘

UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(keyboardHidden)];    tap.numberOfTapsRequired=1;    [self.view addGestureRecognizer:tap];-(void) keyboardHidden{    //注销第一响应者    [_textField resignFirstResponder];}
5.给xlb赋予新值必须在视图展现之前如:

- (void)viewWillAppear:(BOOL)animated


这只是自己开发中感觉经常用到的,后续也可能不断的增加。




最新文章

123

最新摄影

微信扫一扫

第七城市微信公众平台