iOS開發過程中的各種tips
前言
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];處理鍵盤通知
先注冊通知,然后實現具體當鍵盤彈出來要做什么,鍵盤收起來要做什么
- (void)registerForKeyboardNotifications { keyboardShown = NO;//標記當前鍵盤是沒有顯示的 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasShown:) name:UIKeyboardWillShowNotification object:nil]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWasHidden:) name:UIKeyboardDidHideNotification object:nil]; } //鍵盤顯示要做什么
- (void)keyboardWasShown:(NSNotification *)notification { if (keyboardShown) {
} NSDictionary info = [notification userInfo]; NSValue aValue = [info objectForKey:UIKeyboardFrameBeginUserInfoKey]; CGSize keyboardSize = [aValue CGRectValue].size; CGRect viewFrame = scrollView.frame; viewFrame.size.height = keyboardSize.height; CGRect textFieldRect = activeField.frame; [scrollView scrollRectToVisible:textFieldRect animated:YES]; keyboardShown = YES; }return;
- (void)keyboardWasHidden:(NSNotification )notification { NSDictionary info = [notification userInfo]; NSValue aValue = [info objectForKey:UIKeyboardFrameEndUserInfoKey]; CGSize keyboardSize = [aValue CGRectValue].size; CGRect viewFrame = scrollView.frame; viewFrame.size.height += keyboardSize.height; scrollView.frame = viewFrame; keyboardShown = NO; }</pre>
點擊鍵盤的next按鈕,在不同的textField之間換行
- (BOOL)textFieldShouldReturn:(UITextField)textField { if ([textField returnKeyType] != UIReturnKeyDone) {}else {NSInteger nextTag = [textField tag] + 1; UIView *nextTextField = [self.tableView viewWithTag:nextTag]; [nextTextField becomeFirstResponder];
} return YES; }</pre>[textField resignFirstResponder];
總結
不積跬步無以至千里,不積小流無以成江海。(隨時更新)
</div> 原文 http://segmentfault.com/a/1190000003505115