IOS開發的一些小技巧

來源:互聯網
上載者:User

標籤:

 將圖片寫入模擬器相簿

   

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);

 

兩個類之間用資料類屬性傳值,傳值失敗,檢查發現,接收類的資料存放區類(nsArray/nsString...........)開闢了新的記憶體,導致失敗;

 用XIB搭建uiTextField時取消鍵盤失效

 

-(BOOL)textFieldShouldReturn:(UITextField *)textField

 

 

 

失效,代碼也寫好了delegate的,解決辦法是雙重保險,XIB檔案的代理連線也需要檢查。都有就OK了。

擷取控制項相對於螢幕的方法

  

 CGRect cgrect= [textField convertRect:textField.frame                                       toView:self.superview.superview];

 

屬性化字串的使用

效果如右圖,開始時無法用lenght取到冒號後面的nsRange,老說超出範圍;後來一想前面肯定0開始的,所以,直接先將字型設定為橘色,然後取前面的文字還原顏色就好了。

Google後明白,為什麼提示超出範圍了,應為range代表的是,起點以及從起點開始的長度範圍,並不是字串的整體長度;

uiTextView遮擋鍵盤的處理

 

擷取裝置此存及物理解析度

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直接調用方法(不要問LZ為什麼),是用UITableView的執行個體來調用的

 

長按手勢的判定,防止出現兩次

 

修改UITextField的LAYER層時注意需要注意需要取消BorderStyle否則無效

#pragma mark - 如無必要不要重載視圖的生命週期方法,即使寫出來什麼代碼都沒添加

比如viewWillAppear,loadView........否則可能引發嚴重的BUG

載入WEBView的撥號

 

地圖中兩個經緯度間的距離

經高德經緯度測試誤差±500m,其他未測

NSString轉NSDictionary

 

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

 

 使用高德地圖API時提示“apiKey為空白...”  

 [MAMapServices sharedServices].apiKey=MAMapKey;  進入此方法 顯示提示

+ (MAMapServices *)sharedServices;/*!@brief API Key, 在建立MAMapView之前需要先綁定key.*/@property (nonatomic, copy) NSString *apiKey;

 

 確保綁定apiKey寫在第一位位置即可。

 

列印應用的緩衝路徑

NSString *path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) lastObject];NSLog(@"path:%@",path);

 

 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:^{}];

 

使用地理編碼時,列印的資料是: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]);}}];}}

 

 為視圖添加背景圖片並且節約記憶體

    self.view.layer.contents = (id) [UIImage imageNamed:@"table"].CGImage;

 

對類進行重新命名

選中類聲明,然後右鍵,找到rename,把 .h和.m的檔案名稱改成重新命名的名字,然後編譯一下,此時會爆路徑下沒有此檔案的錯誤,那是因為xcode裡雖然改了可是源檔案並沒有改。所以show in finder 找到源檔案改為重新命名的名字編譯即可通過。以上步驟是用來解決網路監測的重新命名問題的。

 

IOS開發的一些小技巧

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.