用以記錄在做iOS項(xiàng)目時(shí)總結(jié)的經(jīng)驗(yàn),讓別人或自己以后少踏入這些坑。或者進(jìn)坑后如何更快脫坑。
1.以前使用Mac自帶的數(shù)碼測(cè)色計(jì)設(shè)置UIColor總是會(huì)有偏差,原來(lái)是取色器設(shè)置的問(wèn)題。
解決方案如下:設(shè)置為顯示原生值(如下圖所示)
2.拖了一個(gè)UILabel控件,對(duì)控件添加了點(diǎn)擊事件卻無(wú)法觸發(fā)點(diǎn)擊事件
解決方案:控件的userInteractionEnabled屬性設(shè)置為YES。 代碼截圖如下:
3.通過(guò)addsubview添加的視圖,UILabel的點(diǎn)擊事件不起作用。
解決方案:在sddsubview前,將添加的視圖控制器在跳轉(zhuǎn)前初始化(重點(diǎn)是要把a(bǔ)dd那個(gè)ViewController定義成屬性,就是@pro......)。
4.Mac mini更新Xcode很慢,很費(fèi)時(shí)間。而且造成機(jī)子在更新過(guò)程中很卡。
描述:更新了一個(gè)下午了,還沒(méi)更新完。
解決方案:更新需謹(jǐn)慎。
5.synthesize
@synthesize會(huì)自動(dòng)生成getter和setter的實(shí)現(xiàn)
@synthesize默認(rèn)會(huì)去訪問(wèn)age,no,height同名的變量,,
如果找不到同名的變量,會(huì)在內(nèi)部自動(dòng)生成一個(gè)私有同名變量age,no,height,,
因此Student.h中的這幾個(gè)變量也可以省略不寫。
6.Git和Subversion的區(qū)別
Subversion的提交:只要提交的內(nèi)容和版本庫(kù)的內(nèi)容不沖突,就允許提交,不管本地work copy是否最新。
Git:必須把遠(yuǎn)程版本庫(kù)的改動(dòng)Pull到本地版本庫(kù)以后才可以把本地的改動(dòng)push到遠(yuǎn)程版本庫(kù)。
7.Mac終端命令的使用
8.self present...跳轉(zhuǎn)出現(xiàn)警告(log打印日志警告)
解決辦法:用:self showViewController:......
9.從xib頁(yè)面跳轉(zhuǎn)到storyboard上
UIStoryboard *storyTest = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *v = [storyTest instantiateViewControllerWithIdentifier:@"mainTest"];
[self presentViewController:v animated:YES completion:nil];
10.Mac升級(jí)到10.11.5后,cocoa pods更新到1.0,不能使用cocoapods的解決辦法
昨天(2016.05.17)下午App Store推薦我升級(jí)到Mac 10.11.5。然后完了,今天使用cocoapods就出錯(cuò)了,怎么配置、更行都不行。
解決方案:
podfile 換格式
platform :ios, '8.0'
use_frameworks!
target 'YourApp' do
pod 'AFNetworking', '~> 2.6'end
11.解決iOS9以后不能使用http請(qǐng)求
解決辦法:(NSAllows... 是NSApp... 的子級(jí))
在info.plist中添加<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/></dict>
第一級(jí)(NSApp...)字典數(shù)據(jù)類型,第二級(jí)(NSAllows...)BOOL類型,設(shè)置為YES就行了
12.系統(tǒng)的json解析
// 系統(tǒng)json解析
// 網(wǎng)絡(luò)路徑
NSURL *urlPathRoot = [NSURL URLWithString:@"http://192.168.0.103:8080/jmj-web-api-x/ipad/order/orderInfo.do?condition=1&pageNo=1"];
// 請(qǐng)求
NSURLRequest *requestRoot = [NSURLRequest requestWithURL:urlPathRoot];
// 網(wǎng)絡(luò)會(huì)話
NSURLSession *sessionRoot = [NSURLSession sharedSession];
// 加載數(shù)據(jù)
NSURLSessionDataTask *dataTask = [sessionRoot dataTaskWithRequest:requestRoot completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) {
NSLog(@"%@",[[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding]);
}];
// 使用resume方法啟動(dòng)任務(wù)
[dataTask resume];
13.警告: Could not load the "" image referenced from a nib in the bundle with identifier "com.jmj.czy.iPad"
描述:打印如上提示
解決方案:xib上的圖片重新設(shè)置
14.多次遇到視圖上的點(diǎn)擊事件不能觸發(fā)
解決方案:在addsubview前,將要add的視圖設(shè)置為屬性,在add前初始化視圖控制器
15.三目運(yùn)算符
*N目運(yùn)算符->三目運(yùn)算符
# int a = 5?10:2;
# 獲得a、b中的最大數(shù)
# 獲得a、b、c中的最大數(shù)
16.通過(guò)tag值獲得相應(yīng)的控件
UIButton *btn = (UIButton *)[self viewWithTag:currentIndex];
17.Xcode工具不能自動(dòng)換行的問(wèn)題
解決方案:如下圖
若還是沒(méi)自動(dòng)換行,就是工程紀(jì)錄了之前的設(shè)置。解決方法如下圖
18.iOS9網(wǎng)絡(luò)請(qǐng)求為http的配置
解決方案如下圖:
一個(gè)dic:NSAppTransportSecurity
下面一個(gè)BOOL:NSAllowsArbitraryLoads(設(shè)為YES)
在plist文件中添加一個(gè)藍(lán)色框內(nèi)的配置就行了
19. 剛才遇到一個(gè)好坑的bug(描述不清了,有點(diǎn)亂)
癥狀:UITableView在滾動(dòng)時(shí),reloadData這個(gè)tableView報(bào)錯(cuò),而且不報(bào)具體原因。
治病方案:poiInfoArr = nil; // 將數(shù)組設(shè)置為空(數(shù)據(jù)源)
[self.tabV reloadData];// 刷新表格,然后添加頭視圖
20.在block語(yǔ)句塊中,不能設(shè)置外部UIButton的title屬性,不變化。
NSURLSessionUploadTask *uploadTask2 = [manager uploadTaskWithRequest:req fromData:nil progress:^(NSProgress * _Nonnull uploadProgress) {
// 已上傳多少m
CGFloat compFl = uploadProgress.completedUnitCount;
compFl = compFl/1024/1024;
// 總共多少m
CGFloat totalFl = uploadProgress.totalUnitCount;
totalFl = totalFl/1024/1024;
// 進(jìn)度
double fractionCompletedDouble = uploadProgress.fractionCompleted;
fractionCompletedDouble = fractionCompletedDouble*100;
NSString *progressStr = [NSString stringWithFormat:@"上傳進(jìn)度:%.0f%%\n%.2fM/%.2fM", fractionCompletedDouble, compFl, totalFl];
AMLog(@"progress:%@", progressStr);
// 調(diào)用主進(jìn)程的方法更新界面,在主進(jìn)程外更新界面常會(huì)引起錯(cuò)誤
[self performSelectorOnMainThread:@selector(setCurrentSelectedUploadImgBtnTitle:)
withObject:progressStr
waitUntilDone:NO];
} completionHandler:
解決方案:
// 用下面的方法改變屬性值。
// 調(diào)用主進(jìn)程的方法更新界面,在主進(jìn)程外更新界面常會(huì)引起錯(cuò)誤
[self performSelectorOnMainThread:@selector(setCurrentSelectedUploadImgBtnTitle:)
withObject:progressStr
waitUntilDone:NO];
21.寫圖片跟隨手勢(shì)的滑動(dòng)移動(dòng)時(shí),圖片一直不停的閃爍。
解決方法:locationInView:后面不能以這個(gè)圖片位view對(duì)象
22.百度地圖使用定位,不能進(jìn)入定位結(jié)果的代理-填坑
解決方法:將BMKLocationService這個(gè)類定義為這個(gè)類的變量,就可以了。
23.iOS 不能調(diào)起微信支付-填坑
微信的分享、登錄都是可以的,但是在調(diào)起微信支付時(shí)不行。
并且,微信支付的統(tǒng)一下單也是可以的,但就是調(diào)起微信支付失敗。
填坑方式:重新導(dǎo)入了微信SDK
24.iOS Warning: Attempt to present <UIAlertController: 0x79117e00> on
有時(shí)使用
self presentViewController:
來(lái)顯示一個(gè)視圖時(shí),卻輸出警告。有時(shí)不能呈現(xiàn)視圖。
解決方法如下:
使用
[self.presentedViewController presentViewController:
就行了
25.iOS 使用MD5加密的教程
第一步:導(dǎo)入頭文件
#import <CommonCrypto/CommonDigest.h>
第二部:把MD5加密寫成方法
#pragma mark MD5加密
- (NSString *)md5:(NSString *)str{
const char *cStr = [str UTF8String];//轉(zhuǎn)換成utf-8
unsigned char result[16];//開辟一個(gè)16字節(jié)(128位:md5加密出來(lái)就是128位/bit)的空間(一個(gè)字節(jié)=8字位=8個(gè)二進(jìn)制數(shù))
CC_MD5( cStr, (int)strlen(cStr), result);
NSMutableString *Mstr = [NSMutableString stringWithCapacity:CC_MD5_DIGEST_LENGTH];
for (int i=0; i<CC_MD5_DIGEST_LENGTH; i++) {
[Mstr appendFormat:@"%02X",result[i]];
}
return Mstr;
}
26.生成二維碼的方法
//生成二維碼
- (void)createCIImageWithText:(NSString*)text{
CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
NSString *string = text;
NSData *data = [string dataUsingEncoding:NSUTF8StringEncoding];
//2.通過(guò)kVO設(shè)置濾鏡傳入數(shù)據(jù)
[filter setValue:data forKey:@"inputMessage"];
//3.生成二維碼
CIImage *iconImage = [filter outputImage];
UIImageView *tempImageView = [[UIImageView alloc]initWithFrame:(CGRectMake(50,50,100,100))];
UIImage *image = [UIImage imageWithCIImage:iconImage];
tempImageView.image= image;
[self.view addSubview:tempImageView];
}