ios 開發小記 (二)

一些開發中的常見問題:html

一、interface builder 使用ios

identity inspector 的key path 能夠用來作圓角按鈕和邊框,不用在代碼裏面寫。多線程

 layer.cornerRadius number  圓角按鈕app

 

二、內存管理
使用代理模式的時候,A 和 B的相互引用之間會循環引用,致使內存沒法回收。 解決方案:可使其中一個引用設置爲weak。
須要注意的是,若是是A持有B,那麼A的生命週期比B長,應該把B持有A的引用設置成weak。
 
 
 
三、導航欄顏色 
navigationBar 默認是透明的,顏色會進行高斯模糊處理。因此直接用RGB顏色賦值,最後的顏色效果會稍微淡一些。
解決方案:最開始的navigationBar設置爲opaque。
 
 
 四、HTTP請求
若是是post請求,而且設置了 httpBody,那麼請求的超時時間就被默認設置爲 240 秒了。
就算你再使用[urlRequest setTimeoutInterval:10]; 也是無效的,咱們能夠再設置完成後再讀取這個值,發現它不會變成10,依然保持240秒。
以上是開發中遇到的問題,在iOS9之後,問題都不在了:

    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];ide

    manager.requestSerializer.timeoutInterval = 10;佈局

這個方法親測有效。(AFNetworking) 
 
 
五、UIViewcontroller 層級
presentViewController 和  UINavigationController的區別:
 
presentViewController 通常用於一個viewcontroller
 
同時,dismiss會讓全部的presents的view所有消失。(go back,不方便)

UINavigationController 更靈活,能夠push,而後pop。
(更新: iOS9中,是能夠在modal的viewController中,再modal一次的viewController的)
 
 
六、 UITableViewCell 自適應
cell.contentView.systemLayoutSizeFittingSize(UILayoutFittingCom pressedSize).height + 1 
這個方法能夠在對cell進行初始化後,對cell的高度進行預先處理。
 
 
七、多線程
其餘線程如何操做(更新)UI。

[self performSelectorOnMainThread:@selector(updateUI:) withObject:image waitUntilDone:YES];post

performSelectorOnMainThread是NSObject的方法,除了能夠更新主線程的數據外,還能夠更新其餘線程的好比: 
用:performSelector:onThread:withObject:waitUntilDone: 
 
 

 

八、UI自動佈局ui

layout有三種方式:Manual layout,Autoresizing,Autolayout。咱們經常使用的可能就後面兩種。url

假設v1是一個不使用autolayout的view,而v2是一個使用autolayout的view,但v1成爲v2的subview時,spa

v2須要四條隱含的constraint來肯定v1的位置,這些約束都是從v1的frame轉化而來:

This conversion is performed only if the view in question has its translates-

AutoresizingMaskIntoConstraints property set to YES. That is, in fact, the default if

the view came into existence either in code or by instantiation from a nib where 「Use

Auto Layout」 is not checked. The assumption is that if a view came into existence in

either of those ways, you want its frame and autoresizingMask to act as its constraints

 
if it becomes involved in autolayout. 
 
 
 
 
 
 
 九、UI數據傳遞
能不能在destination controller 中調用prepareForSegue 來回傳數據?
不能。
由於:prepareForSegue只有當將一個controller放到堆棧上面的時候可使用,若是將一個controller從堆棧上面移除,是沒法使用的,具備單向性。
 
self.navigationController popToRootViewControllerAnimated:YES]
 
 

UIViewController *prevVC = [self.navigationController.viewControllers objectAtIndex:<n>];
[self.navigationController popToViewController:prevVC animated:YES];

[self.navigationController popViewControllerAnimated:YES];

 (那麼能夠經過單例、代理模式、notify等各類形式傳遞)

 

 

十、UIViewoController的關係

移出孩子的操做
If you are implementing your own container view controller, it must call the  willMoveToParentViewController: method of the child view controller before calling the  removeFromParentViewController method...

Thus, as discussed in Adding and Removing a Child section of the View Controller Programming Guide, when removing a child, we should:


[childVC willMoveToParentViewController:nil];
[childVC.view removeFromSuperview];
[childVC removeFromParentViewController];
相關文章
相關標籤/搜索