用以記錄在做iOS項目時總結(jié)的經(jīng)驗,讓別人或自己以后少踏入這些坑?;蛘哌M(jìn)坑后如何更快脫坑。
1.以前使用Mac自帶的數(shù)碼測色計設(shè)置UIColor總是會有偏差,原來是取色器設(shè)置的問題。
解決方案如下:設(shè)置為顯示原生值(如下圖所示)

2.拖了一個UILabel控件,對控件添加了點擊事件卻無法觸發(fā)點擊事件
解決方案:控件的userInteractionEnabled屬性設(shè)置為YES。 代碼截圖如下:

3.通過addsubview添加的視圖,UILabel的點擊事件不起作用。



解決方案:在sddsubview前,將添加的視圖控制器在跳轉(zhuǎn)前初始化(重點是要把a(bǔ)dd那個ViewController定義成屬性,就是@pro......)。
4.Mac mini更新Xcode很慢,很費時間。而且造成機(jī)子在更新過程中很卡。
描述:更新了一個下午了,還沒更新完。

解決方案:更新需謹(jǐn)慎。
5.synthesize
@synthesize會自動生成getter和setter的實現(xiàn)
@synthesize默認(rèn)會去訪問age,no,height同名的變量,,
如果找不到同名的變量,會在內(nèi)部自動生成一個私有同名變量age,no,height,,
因此Student.h中的這幾個變量也可以省略不寫。
6.Git和Subversion的區(qū)別
Subversion的提交:只要提交的內(nèi)容和版本庫的內(nèi)容不沖突,就允許提交,不管本地work copy是否最新。
Git:必須把遠(yuǎn)程版本庫的改動Pull到本地版本庫以后才可以把本地的改動push到遠(yuǎn)程版本庫。
7.Mac終端命令的使用
8.self present...跳轉(zhuǎn)出現(xiàn)警告(log打印日志警告)


解決辦法:用:self showViewController:......

9.從xib頁面跳轉(zhuǎn)到storyboard上

UIStoryboard *storyTest = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
ViewController *v = [storyTest instantiateViewControllerWithIdentifier:@"mainTest"];
[self presentViewController:v animated:YES completion:nil];
10.Mac升級到10.11.5后,cocoa pods更新到1.0,不能使用cocoapods的解決辦法
昨天(2016.05.17)下午App Store推薦我升級到Mac 10.11.5。然后完了,今天使用cocoapods就出錯了,怎么配置、更行都不行。

解決方案:
podfile 換格式
platform :ios, '8.0'
use_frameworks!
target 'YourApp' do
pod 'AFNetworking', '~> 2.6'end

11.解決iOS9以后不能使用http請求
解決辦法:(NSAllows... 是NSApp... 的子級)
在info.plist中添加<key>NSAppTransportSecurity</key><dict> <key>NSAllowsArbitraryLoads</key> <true/></dict>
第一級(NSApp...)字典數(shù)據(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"];
// 請求
NSURLRequest *requestRoot = [NSURLRequest requestWithURL:urlPathRoot];
// 網(wǎng)絡(luò)會話
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方法啟動任務(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.多次遇到視圖上的點擊事件不能觸發(fā)
解決方案:在addsubview前,將要add的視圖設(shè)置為屬性,在add前初始化視圖控制器

15.三目運算符
*N目運算符->三目運算符
# int a = 5?10:2;
# 獲得a、b中的最大數(shù)
# 獲得a、b、c中的最大數(shù)
16.通過tag值獲得相應(yīng)的控件
UIButton *btn = (UIButton *)[self viewWithTag:currentIndex];
17.Xcode工具不能自動換行的問題
解決方案:如下圖

若還是沒自動換行,就是工程紀(jì)錄了之前的設(shè)置。解決方法如下圖

18.iOS9網(wǎng)絡(luò)請求為http的配置
解決方案如下圖:
一個dic:NSAppTransportSecurity
下面一個BOOL:NSAllowsArbitraryLoads(設(shè)為YES)
在plist文件中添加一個藍(lán)色框內(nèi)的配置就行了
19. 剛才遇到一個好坑的bug(描述不清了,有點亂)
癥狀:UITableView在滾動時,reloadData這個tableView報錯,而且不報具體原因。
治病方案:poiInfoArr = nil; // 將數(shù)組設(shè)置為空(數(shù)據(jù)源)
[self.tabV reloadData];// 刷新表格,然后添加頭視圖
20.在block語句塊中,不能設(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)程外更新界面常會引起錯誤
[self performSelectorOnMainThread:@selector(setCurrentSelectedUploadImgBtnTitle:)
withObject:progressStr
waitUntilDone:NO];
} completionHandler:
解決方案:
// 用下面的方法改變屬性值。
// 調(diào)用主進(jìn)程的方法更新界面,在主進(jìn)程外更新界面常會引起錯誤
[self performSelectorOnMainThread:@selector(setCurrentSelectedUploadImgBtnTitle:)
withObject:progressStr
waitUntilDone:NO];
21.寫圖片跟隨手勢的滑動移動時,圖片一直不停的閃爍。
解決方法:locationInView:后面不能以這個圖片位view對象
22.百度地圖使用定位,不能進(jìn)入定位結(jié)果的代理-填坑
解決方法:將BMKLocationService這個類定義為這個類的變量,就可以了。
23.iOS 不能調(diào)起微信支付-填坑
微信的分享、登錄都是可以的,但是在調(diào)起微信支付時不行。
并且,微信支付的統(tǒng)一下單也是可以的,但就是調(diào)起微信支付失敗。
填坑方式:重新導(dǎo)入了微信SDK

24.iOS Warning: Attempt to present <UIAlertController: 0x79117e00> on
有時使用
self presentViewController:
來顯示一個視圖時,卻輸出警告。有時不能呈現(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];//開辟一個16字節(jié)(128位:md5加密出來就是128位/bit)的空間(一個字節(jié)=8字位=8個二進(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.通過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];
}
