前言
iOS开发过程中,各种小问题,小技巧,代码片段, 在这里收集,写下,方便查阅,有时间会将其分类,并添加导航和标签。
PS:查找东西,现在只能用com+f,搜索关键字查找。。。
本文应该不在更新,或者说不在blog中更新了,有点麻烦,我会在Github中继续更新,链接地址
返回输入键盘
1 | <UITextFieldDelegate> |
CGRect
1 | CGRectFromString(<#NSString *string#>)//有字符串恢复出矩形 |
隐藏状态栏
1 | [UIApplication sharedApplication] setStatusBarHidden:<#(BOOL)#> withAnimation:<#(UIStatusBarAnimation)#>//隐藏状态栏 |
自动适应父视图大小
1 | self.view.autoresizesSubviews = YES; |
UITableView的一些方法
1 | 这里我自己做了个测试,缩进级别设置为行号,row越大,缩进越多 |
把plist文件中的数据赋给数组
1 | NSString *path = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"]; |
获取触摸的点
1 | - (CGPoint)locationInView:(UIView *)view; |
获取触摸的属性
1 | @property(nonatomic,readonly) NSTimeInterval timestamp; |
从plist中获取数据赋给字典
1 | NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"plist"]; |
NSUserDefaults注意事项
1 | 设置完了以后如果存储的东西比较重要的话,一定要同步一下 |
获取Documents目录
1 | NSString *documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]; |
获取tmp目录
1 | NSString *tmpPath = NSTemporaryDirectory(); |
利用Safari打开一个链接
1 | NSURL *url = [NSURL URLWithString:@"http://baidu.com"]; |
利用UIWebView显示pdf文件,网页等等
1 | <UIWebViewDelegate> |
UIWebView和html的简单交互
1 | myWebView = [[UIWebView alloc]initWithFrame:self.view.bounds]; |
汉字转码
1 |
|
处理键盘通知
1 |
|
点击键盘的next按钮,在不同的textField之间换行
1 | - (BOOL)textFieldShouldReturn:(UITextField *)textField { |
设置日期格式
1 | dateFormatter = [[NSDateFormatter alloc]init]; |
加载大量图片的时候,可以使用
1 | NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"icon" ofType:@"png"]; |
有时候在iPhone游戏中,既要播放背景音乐,同时又要播放比如枪的开火音效。
1 | NSString *musicFilePath = [[NSBundle mainBundle] pathForResource:@"xx" ofType:@"wav"]; |
从通讯录中读取电话号码,去掉数字之间的-
1 | NSString *originalString = @"(123)123123abc"; |
字符串是否为空
1 |
|
正则判断:字符串只包含字母和数字
1 |
|
设置UITableView
的滚动条颜色
1 | self.tableView.indicatorStyle = UIScrollViewIndicatorStyleWhite; |
网络编程
开发web等网络应用程序的时候,需要确认网络环境,连接情况等信息。如果没有处理它们,是不会通过apple的审查的。
系统自带的网络检查是原生的,AFNetworking也为我们添加了相关检测机制,所以这个直接在介绍AFNetworking的时候详解吧。
使用NSURLConnection
下载数据
1 | 1. 创建对象 |
隐藏状态栏
1 | [UIApplication sharedApplication].statusBarHidden = YES; |
.m文件与.mm文件的区别
1 | .m文件是objective-c文件 |
Safari其实没有把内存的缓存写到存储卡上
读取一般性文件
1 | - (void)readFromTXT { |
隐藏UINavigationBar
1 | [self.navigationController setNavigationBarHidden:YES animated:YES]; |
调用电话,短信,邮件
1 | [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"mailto:apple@mac.com?Subject=hello"]]; |
获取版本信息
1 | UIDevice *myDevice = [UIDevice currentDevice]; |
UIWebView
的使用
1 | <UIWebViewDelegate> |
UIButton
的title
和image
不能同时显示UINavigationItem
也是
不要再语言包里面设置空格
NSNotificationCenter
带参数发送
1 | MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc]initWithContentURL:[NSURL fileURLWithPath:moviePath]]; |
延迟一段时间执行某个函数
1 | [self performSelector:@selector(dismissModal) withObject:self afterDelay:1.0]; |
用NSDateFormatter
调整时间格式代码
1 | NSDateFormatter *dateFormatter = [[NSDateFormatter alloc]init]; |
UIView
设置成圆角的方法
1 | mainView.layer.cornerRadius = 6; |
Objective-C 内存管理
- 一个对象可以有一个或多个拥有者
- 当它一个拥有者都没有的时候,它就会被回收
- 如果想保留一个对象不被回收,你就必须成为它的拥有者
关键字
- alloc
为对象分配内存,计数设为1,并返回此对象。
- copy
复制一个对象,此对象计数为1,返回此对象。你将成为此克隆对象的拥有者。 - retain
对象计数+1,并成为次对象的拥有者。 - release
对象计数-1,并丢掉此对象。 - autorelease
在未来的某一个时刻,对象计数-1。并在未来的某个时间放弃此对象。
原则
- 一个代码块内要确保copy,alloc 和 retain 的使用数量与 release 和 autorelease 的数量相等。
- 在使用以 alloc 或 new 开头或包含 copy 的方法,或 retain 一个对象时,你将会编程它的拥有者。
- 实现 dealloc 方法,这是系统当 retain -> 0 的时候,自动调用的。手动调用会引起 retain count 计数错误(多一次的 release)。
iPhone 更改键盘右下角按键的 type
1 | SearchBar *mySearchBar = [[UISearchBar alloc]init]; |
tableView左滑出多个按钮
1 | - (NSArray *)tableView:(UITableView *)tableView editActionsForRowAtIndexPath:(NSIndexPath *)indexPath |
给图片增加模糊效果
1 | //加模糊效果,image是图片,blur是模糊度 |
网易新闻头部滚动切换
1 | - (void)titleButtonDidClick:(UIButton *)titleButton |
七牛上传图片的简单封装
1 |
|
导航条渐隐渐现
1 | [_tableView addObserver: self forKeyPath: @"contentOffset" options: NSKeyValueObservingOptionNew context:nil]; |
图片压缩
1 | 用法:UIImage *yourImage= [self imageWithImageSimple:image scaledToSize:CGSizeMake(210.0, 210.0)]; |
图片裁剪,类似新浪微博小图显示效果
1 |
|
把时间戳转换为时间
1 | + (NSDate *)dateWithTimeIntervalInMilliSecondSince1970:(double)timeIntervalInMilliSecond { |
自定义cell中获取不到cell实际大小的办法
1 | -(void)drawRect:(CGRect)rect { |
长按图标抖动
1 | -(void)longPress:(UILongPressGestureRecognizer*)longPress |
阿拉伯数字转化为汉语数字
1 | +(NSString *)translation:(NSString *)arebic |
两种方法删除NSUserDefaults所有记录
1 | //方法一 |
截屏 全图
1 | - (UIImage *)imageFromView: (UIView *) theView |
判断是否用户开启了定位服务
1 | if ([CLLocationManager locationServicesEnabled] && |
图片旋转
1 |
|
去除UIImageView锯齿
1 |
|
判断两个日期之间的间隔
1 | NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init]; |
类似微信发送视频的流程
1 | //获取视频的本地url或者path,对视频进行获取第一帧图片,然后初始化消息的Model,设置封面 |
刷新某行cell的方法
1 | //有时候只需要刷新某行的cell的数据,完全没必要调用[tableView reloadData]刷新整个列表的数据,调用以下方法即可。 |
由身份证号码返回性别
1 | -(NSString *)sexStrFromIdentityCard:(NSString *)numberStr{ |
数组随机重新排列
1 | + (NSArray *)getRandomWithPosition:(NSInteger)position positionContent:(id)positionContent array:(NSArray *)baseArray { |
利用陀螺仪实现更真实的微信摇一摇动画
1 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions |
GIF图片解析
1 | + (NSMutableArray *)praseGIFDataToImageArray:(NSData *)data; |
在后台播放音乐
1 | //1. 在Info.plist中,添加"Required background modes"键,其值设置是“App plays audio" |