將圖片寫入模擬器相冊
UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:@"call"], nil, nil, nil);
UIView* uiViewBuildingAddress=[[UIViewalloc]initWithFrame:self.view.frame]; uiViewBuildingAddress.backgroundColor=[UIColorblackColor]; //父視圖不影響子視圖背景色 uiViewBuildingAddress.backgroundColor=[UIColorcolorWithWhite:0 alpha:0.6];
#PRagma mark - 獲取view從屬的viewController
- (UIViewController*)getViewController{ for (UIView* next = [selfsuperview]; next; next = next.superview) { UIResponder* nextResponder = [next nextResponder]; if ([nextResponderisKindOfClass:[UIViewControllerclass]]) { return (UIViewController*)nextResponder; } } return nil;} - (UIViewController*)getViewController{while ([[self nextResponder] isKindOfClass:[UIViewController class]]){return (UIViewController*)[self nextResponder];}return nil;}
避免Block的Retain Cycle
1. __block ASIHTTPRequest* request=[ASIHTTPRequestrequestWithURL:url]; __weak ASIHTTPRequest* request2=request; 2. ASIHTTPRequest* request=[ASIHTTPRequestrequestWithURL:url]; ASIHTTPRequest* __weak request2=request;
#pragma mark - 返回當前時間精確到秒作為圖片名
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);
兩個類之間用數(shù)據(jù)類屬性傳值,傳值失敗,檢查發(fā)現(xiàn),接收類的數(shù)據(jù)存儲類(nsArray/nsString...........)開辟了新的內(nèi)存,導致失敗;
用XIB搭建uiTextField時取消鍵盤失效
-(BOOL)textFieldShouldReturn:(UITextField *)textField失效,代碼也寫好了delegate的,解決辦法是雙重保險,XIB文件的代理連線也需要檢查。都有就OK了。
屬性化字符串的使用


效果如右圖,開始時無法用lenght取到冒號后面的nsRange,老說超出范圍;后來一想前面肯定0開始的,所以,直接先將字體設置為橘色,然后取前面的文字還原顏色就好了。
Google后明白,為什么提示超出范圍了,應為range代表的是,起點以及從起點開始的長度范圍,并不是字符串的整體長度;
uiTextView遮擋鍵盤的處理
-(BOOL)textView:(nonnull UITextView *)textViewshouldChangeTextInRange:(NSRange)rangereplacementText:(nonnull NSString *)text{ if ([text isEqualToString:@"/n"]) { [textView resignFirstResponder]; return NO; } return YES;}
返回值文檔如下描述
Return Value
YES true if the old text should be replaced by the new text; NO false if the replacement Operation should be aborted.
返回yes輸入的字符生效,返回no輸入的字符失效
使用此方法時莫名出現(xiàn)以下錯誤(xcode7,iphone5s)
-[UITextEffectsWindow _adjustPointForHostedDisplay:hasTarget:inset:]: unrecognized selector sent to instance
所以可以使用另外一種鍵盤回收方法
UIToolbar* tool=[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 30)]; tool.barStyle=UIBarStyleDefault; UIBarButtonItem* keyReturn=[[UIBarButtonItem alloc] initWithBarButtonSystemItem:(UIBarButtonSystemItemDone ) target:self action:@selector(keyBoardReturn)];
//多個操作按鈕需使用空格按鈕 tool.items=@[keyReturn]; reason.inputaccessoryView=tool;
/** * textview回收鍵盤 */-(void)keyBoardReturn{ [reason resignFirstResponder];}
為textview設置一個附加視圖,附加視圖添加取消鍵盤響應的方法即可
獲取設備此存及物理分辨率
NSString* height = [NSString stringWithFormat:@"%d", (NSInteger)[UIScreen mainScreen].currentMode.size.height]; NSString* width = [NSString stringWithFormat:@"%d", (NSInteger)[UIScreen mainScreen].currentMode.size.width]; #define CgrectUIScreen [UIScreen mainScreen].bounds#define UIScreenWide [UIScreen mainScreen].bounds.size.width#define UIScreenHeight [UIScreen mainScreen].bounds.size.height
屏蔽NSLog
#define NSLog(...) {}; #endif
獲取UItableView的某個Cell,千萬注意紅色的部分,不要用self直接調(diào)用方法(不要問LZ為什么),是用UITableView的實例來調(diào)用的

長按手勢的判定,防止出現(xiàn)兩次

修改UITextField的LAYER層時注意需要注意需要取消BorderStyle否則無效
#pragma mark - 如無必要不要重載視圖的生命周期方法,即使寫出來什么代碼都沒添加
比如viewWillAppear,loadView........否則可能引發(fā)嚴重的BUG
加載WEBView的撥號

地圖中兩個經(jīng)緯度間的距離

經(jīng)高德經(jīng)緯度測試誤差±500m,其他未測
NSString轉NSDictionary

uitabeleviewCell添加了uitextfield無法呼出鍵盤,在模擬器的工具欄找到如下選項,嘗試去掉第二選項(連接硬件鍵盤)

使用高德地圖API時提示“apiKey為空...”
[MAMapServices sharedServices].apiKey=MAMapKey; 進入此方法 顯示提示
+ (MAMapServices *)sharedServices;/*!@brief API Key, 在創(chuàng)建MAMapView之前需要先綁定key.*/@property (nonatomic, copy) NSString *apiKey;
確保綁定apiKey寫在第一位位置即可。
打印應用的緩存路徑
NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSLog(@"path:%@",path);PLIST文件的一般使用
//創(chuàng)建或者寫入**文件絕對路徑,如果文件有數(shù)據(jù)直接使用即可 NSString *filepath=[(NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0]) stringByAppendingPathComponent:@"userInform.plist"]; NSDictionary*userInform=[NSDictionary dictionaryWithContentsOfFile:filepath]; [userInform writeToFile:filepath atomically:YES];
storyBoard代碼跳轉
1.通過storyBoard里已有的segue,設置segue的Identifier.
[self performSegueWithIdentifier:@"SegueName" sender:self];
2.通過storyBoard里設置storyBoardID.
UIStoryboard* uiStoryboardMain=[UIStoryboard storyboardWithName:@"StoryboardMain" bundle:nil];UIViewController* uiViewController=[uiStoryboardMain instantiateViewControllerWithIdentifier:@"tabBarController"];[self presentViewController:uiViewControlleranimated:YEScompletion:^{}];注意勾選此選項

使用地理編碼時,打印的數(shù)據(jù)是:Name = "2nd Ring Road Elevated Road Yulin Residential District Wuhou Chengdu Sichuan China";明顯的是英式翻譯,那么就是本地化的問題,google后得知,應該將模擬器設置為中文語言就沒有問題了。 Name = "/U822a/U7a7a/U8def";但是需要糾正偏差才準確。
-(void)locationManager:(CLLocationManager *)managerdidUpdateLocations:(NSArray *)locations{if (locations.count>0){CLLocation* latesLocation=[locations lastObject];// NSLog(@"latesLocation:%@",latesLocation);// mapCenter(latesLocation.coordinate);CLGeocoder* geocoder=[[CLGeocoder alloc]init];[geocoder reverseGeocodeLocation:latesLocationcompletionHandler:^(NSArray *placemarks, NSError *error){if (placemarks.count>0){CLPlacemark* latesPlaceMark=(CLPlacemark*)[placemarks lastObject];NSLog(@"latesPlaceMark.addressDictionary:%@",(latesPlaceMark.addressDictionary[@"FormattedAddressLines"])[0]);}}];}}
為視圖添加背景圖片并且節(jié)約內(nèi)存
self.view.layer.contents = (id) [UIImage imageNamed:@"table"].CGImage;
對類進行重命名

選中類聲明,然后右鍵,找到rename,把 .h和.m的文件名改成重新命名的名字,然后編譯一下,此時會爆路徑下沒有此文件的錯誤,那是因為xcode里雖然改了可是源文件并沒有改。所以show in finder 找到源文件改為重新命名的名字編譯即可通過。以上步驟是用來解決網(wǎng)絡監(jiān)測的重命名問題的。
顯示狀態(tài)欄[xib配置無效只好手寫]
在viewcontroller.m文件中按需配置
/** * 狀態(tài)欄風格設置 * * @return */-(UIStatusBarStyle)preferredStatusBarStyle{ return UIStatusBarStyleLightContent;}/** * 顯示狀態(tài)欄 * * @return */-(BOOL)prefersstatusBarHidden{ return NO;}
UITableViewStyleGrouped 頭部多余一部分怎么取消呢

UIView *view = [[UIView alloc]init]; view.frame = CGRectMake(CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN, CGFLOAT_MIN); view.backgroundColor = [UIColor clearColor]; [tableView setTableHeaderView:view]; [tableView setTableFooterView:view];
以空白視圖替換頭部視圖
設置tabbaritem的圖片尺寸最好是32*32pixels,否則會使圖片溢出或者遮擋住item的文字

對UIDatePicker選擇的日期進行 format。1)當你的format格式是NSDateFormatter* dateFormatter = [[[NSDateFormatter alloc] init] autorelease];[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"]; // 這里是用大寫的 HNSString* dateStr = [dateFormatter stringFromDate:date];你獲得就是24小時制的。2)當你的format格式用的是[dateFormatter setDateFormat:@"yyyy-MM-dd hh:mm:ss"]; // 這里是用小寫的 h你獲得的就是12小時制的。
限制輸入長度
-(BOOL)textField:(nonnull UITextField *)textFieldshouldChangeCharactersInRange:(NSRange)rangereplacementString:(nonnull NSString *)string{// NSLog(@"%@",string); //可變 NSMutableString *string0 = [NSMutableString stringWithString:textField.text]; //替換成字符形式這是必須的步驟 [string0 replaceCharactersInRange:range withString:string]; if (string0.length>11) { return NO; } return YES;}
模擬器上使用CMD+shift+h可以達到HOME鍵的效果,多次使用可達到多次使用HOME鍵的效果
約束排錯
有時候即使xib中沒有約束錯誤,可是運行時依舊會提示約束錯誤

通常情況下是第二個紅框提示有用,但是像現(xiàn)在這樣未指明是哪個控件就比較無力,那么就需要仔細觀察第一個框所在的區(qū)域,因為此區(qū)域通常包含的是引起bug的相關約束,可以直接找到成為突破口。
新聞熱點
疑難解答