iOS開發過程中的各種tips

jopen 9年前發布 | 12K 次閱讀 IOS iOS開發 移動開發

前言

iOS開發過程中,總有那么一些個小問題讓人糾結,它們不會讓程序崩潰,但是會讓人崩潰。除此之外,還將分享一些細節現在我通過自己的總結以及從其他地方的引用,來總結一下一些常見小問題。

本篇長期更新,多積累,多奉獻,同時感謝其中一些文章的作者的整理,感謝!

iOS高級開發實戰講解

這是我在網上搜索到的 iOS高級開發實戰講解 ,由于原文不是很方便瀏覽,所以我在這里整理一部分出來,方便查閱,同時謝謝原作者。

這里我不是每一個都收錄進來,這里只是放出一部分,有些用的太多,我就沒整理了,大家如果想看可以去看原文。

返回輸入鍵盤

<UITextFieldDelegate>

-(BOOL)textFieldShouldReturn:(UITextField *)textField { [textField resignFirstResponder]; return YES; }</pre>

CGRect

CGRectFromString(<#NSString *string#>)//有字符串恢復出矩形
CGRectInset(<#CGRect rect#>, <#CGFloat dx#>, <#CGFloat dy#>)//創建較小或者較大的矩形
CGRectIntersectsRect(<#CGRect rect1#>, <#CGRect rect2#>)//判斷兩巨星是否交叉,是否重疊
CGRectZero//高度和寬度為零的,位于(0,0)的矩形常量

隱藏狀態欄

[UIApplication sharedApplication] setStatusBarHidden:<#(BOOL)#> withAnimation:<#(UIStatusBarAnimation)#>//隱藏狀態欄

自動適應父視圖大小

self.view.autoresizesSubviews = YES;
    self.view.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;

UITableView的一些方法

這里我自己做了個測試,縮進級別設置為行號,row越大,縮進越多
<UITableViewDelegate>

  • (NSInteger)tableView:(UITableView )tableView indentationLevelForRowAtIndexPath:(NSIndexPath )indexPath { NSInteger row = indexPath.row; return row; }</pre>

    把plist文件中的數據賦給數組

    NSString *path = [[NSBundle mainBundle] pathForResource:@"States" ofType:@"plist"];
    NSArray *array = [NSArray arrayWithContentsOfFile:path];

    獲取觸摸的點

    - (CGPoint)locationInView:(UIView *)view;
  • (CGPoint)previousLocationInView:(UIView )view;</pre>

    獲取觸摸的屬性

    @property(nonatomic,readonly) NSTimeInterval      timestamp;
    @property(nonatomic,readonly) UITouchPhase        phase;
    @property(nonatomic,readonly) NSUInteger          tapCount;

    從plist中獲取數據賦給字典

    NSString plistPath = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"plist"];
    NSDictionary dictionary = [NSDictionary dictionaryWithContentsOfFile:plistPath];</pre> 

    NSUserDefaults注意事項

    設置完了以后如果存儲的東西比較重要的話,一定要同步一下
    [[NSUserDefaults standardUserDefaults] synchronize];

    獲取Documents目錄

    NSString documentsDirectory = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];</pre> 

    獲取tmp目錄

    NSString *tmpPath = NSTemporaryDirectory();

    利用Safari打開一個鏈接

    NSURL *url = [NSURL URLWithString:@"http://baidu.com"];
    [[UIApplication sharedApplication] openURL:url];

    利用UIWebView顯示pdf文件,網頁等等

    <UIWebViewDelegate>

UIWebView *webView = [[UIWebView alloc]initWithFrame:self.view.bounds]; webView.delegate = self; webView.scalesPageToFit = YES; webView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight; [webView setAllowsInlineMediaPlayback:YES]; [self.view addSubview:webView];

NSString pdfPath = [[NSBundle mainBundle] pathForResource:@"book" ofType:@"pdf"]; NSURL url = [NSURL fileURLWithPath:pdfPath]; NSURLRequest *request = [NSURLRequest requestWithURL:url cachePolicy:(NSURLRequestUseProtocolCachePolicy) timeoutInterval:5]; [webView loadRequest:request];</pre>

UIWebView和html的簡單交互

myWebView = [[UIWebView alloc]initWithFrame:self.view.bounds];
[myWebView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:@"error;
NSString errorString = [NSString stringWithFormat:@"<html><center><font size=+5 color='red'>AnError Occurred;<br>%@</font></center></html>",error];
[myWebView loadHTMLString:errorString baseURL:nil];

//頁面跳轉了以后,停止載入 -(void)viewWillDisappear:(BOOL)animated { if (myWebView.isLoading) { [myWebView stopLoading]; } myWebView.delegate = nil; [UIApplication sharedApplication].networkActivityIndicatorVisible = NO; }</pre>

漢字轉碼

NSString *oriString = @"\u67aa\u738b";
NSString *escapedString = [oriString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

處理鍵盤通知

先注冊通知,然后實現具體當鍵盤彈出來要做什么,鍵盤收起來要做什么

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!