Objective-C使用部分技巧

Objective-C使用部分技巧,第1张

概述本文章向大家介绍Objective-C使用部分技巧,主要包括Objective-C使用部分技巧使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

一:避免Block的Retain Cycle

1.__block ASIhttpRequest* request=[ASIhttpRequestrequestWithURL:url];

__weak ASIhttpRequest* request2=request;

2.ASIhttpRequest* request=[ASIhttpRequestrequestWithURL:url];

ASIhttpRequest* __weak request2=request;

二:返回当前时间精确到秒作为图片名

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

//[formatter setDateFormat:@"YYYY.MM.dd.hh.mm.ss"];//秒

//[formatter setDateFormat:@"YYYY-MM-dd hh:mm:ss:SSS"];//毫秒

[formatter setDateFormat:@"YYYYMMddhhmmssSSS"];

Nsstring* nsstringCurrentTime = [formatter stringFromDate:[NSDate date]];

//NSLog(@"当前时间:%@",nsstringCurrentTime);

三:屏蔽NSLog

#define NSLog(...) {};

#endif

四:长按手势的判定,防止出现两次

- (voID)longPressGestureRecognizer:(UILongPressGestureRecognizer *)sender {

//对手势状态进行判断 否则出现两次

if (sender.state == UIGestureRecognizerStateEnded){

UIActionSheet *actionSheet =[ [UIActionSheet alloc] initWithTitle:nil delegate:self cancelbuttonTitle:@"不了" destructivebuttonTitle:nil otherbuttonTitles:@"删除草稿",@"清空草稿箱",nil];

//将cell的Tag值传给actionSheet,确定哪个cell触发了行为

actionSheet.tag = sender.vIEw.tag;

[actionSheet showInVIEw:self.tabbarController.vIEw];

}

}

五:如无必要不要重载视图的生命周期方法,即使写出来什么代码都没添加

比如vIEwWillAppear,loadVIEw........否则可能引发严重的BUG

六:UItableVIEwStyleGrouped 头部多余一部分的取消

UIVIEw *vIEw = [[UIVIEw alloc]init];

vIEw.frame = CGRectMake(CGfloat_MIN,CGfloat_MIN,CGfloat_MIN);

vIEw.backgroundcolor = [UIcolor clearcolor];

[tableVIEw settableheaderVIEw:vIEw];

[tableVIEw settableFooterVIEw:vIEw];

以空白视图替换头部视图

七:对UIDatePicker选择的日期进行 format。

对UIDatePicker选择的日期进行 format。

1)当你的format格式是

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

[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 这里是用大写的 H

Nsstring* dateStr = [dateFormatter stringFromDate:date];

你获得就是24小时制的。

2)当你的format格式用的是

[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; // 这里是用小写的 h

你获得的就是12小时制的。

八:限制输入长度

-(BOol)textFIEld:(nonnull UITextFIEld *)textFIEld

shouldChangeCharactersInRange:(NSRange)range

replacementString:(nonnull Nsstring *)string

{

// NSLog(@"%@",string);

//可变

NSMutableString *string0 = [NSMutableString stringWithString:textFIEld.text];

//替换成字符形式这是必须的步骤

[string0 replaceCharactersInRange:range

withString:string];

if (string0.length>11)

{

return NO;

}

return YES;

}

总结

以上是内存溢出为你收集整理的Objective-C使用部分技巧全部内容,希望文章能够帮你解决Objective-C使用部分技巧所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1264347.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存