一、@try和@catch的使用
// NSArray *arr1 = @[@1, @2, @3];
NSArray *arr1 = @[@"1", @"2", @"3"];
NSArray *arr2 = @[@"1", @"2", @"3"];
@try { //業務邏輯代碼
if ([arr1[1] isEqualToString:arr2[2]]) {
NSLog(@"相等");
} else {
NSLog(@"不相等");
}
} @catch (NSException *exception) { //處理異常的代碼
NSLog(@"錯誤日誌:%@", exception);
} @finally {
NSLog(@"*******成功失敗都會執行********");
}
注意:finally語句不管是否有異常都會執行;如果try裏面出現異常,則立即跳到catch中,try後面的代碼不會被執行,程序會繼續運行;
如果try裏面的異常在catch沒有所匹配的,則程序會崩潰。
二、UIView的佈局和重繪
- (void)layoutSubviews
應用於佈局約束,調用的次數非常頻繁。只要view的frame發生了改變都會被調用,且addSubView方法也會觸發去調用。用於在子類中重寫,不能直接調用。
- (void)setNeedsLayout
網上說是一個動畫標記,具體怎麼用不知道。- (void)layoutIfNeeded
當我們用xib佈局view時,把一個約束拖入到demo中,如:約束NSLayoutConstraint *rightX。通過UIView動畫改變rightX值時,無法獲得動畫效果,需要在動畫方法裏面加上layoutIfNeeded即可進行動畫。
- (void)setNeedsDisplay
- (CGSize)sizeThatFits:(CGSize)size
- (void)sizeToFit
- (void)drawRect:(CGRect)rect
三、關於 #import <notify.h> 框架的使用
用於監聽手機鎖屏
四、視圖的層次添加關係:bringSubviewToFront
將v1調整到父視圖的最上面
[self.view bringSubviewToFront:v1];
[self.view sendSubviewToBack:v3];
五、IBInspectable和IBDesignable的使用
IBInspectable:可視化編輯xib中控件的屬性,比如:圓角、邊框、邊框顏色
IBDesignable:動態渲染該類圖形化界面,即可以讓IBInspectable添加的屬性,在xib中可以動態編輯預覽
#import <UIKit/UIKit.h>
IB_DESIGNABLE
@interface ZMView : UIView
/*************IBInspectable 可視化編輯該類的屬性***************/
// 圓角半徑
@property (nonatomic, assign) IBInspectable CGFloat cornerRadius;
// 邊寬
@property (nonatomic, assign)IBInspectable CGFloat bwidth;
// 邊的顏色
@property (nonatomic, assign)IBInspectable UIColor *bcolor;
@end
#import "ZMView.h"
@implementation ZMView
- (void)setCornerRadius:(CGFloat)cornerRadius {
_cornerRadius = cornerRadius;
self.layer.cornerRadius = cornerRadius;
self.layer.masksToBounds = YES;
}
- (void)setBcolor:(UIColor *)bcolor{
_bcolor = bcolor;
self.layer.borderColor = _bcolor.CGColor;
}
- (void)setBwidth:(CGFloat)bwidth {
_bwidth = bwidth;
self.layer.borderWidth = _bwidth;
}
@end
若xib中某個控件與ZMView類相關連,那麼它的屬性裏面會多出我們上面添加的屬性,用戶就可以可視化的去編輯。
六、我在使用“git.oschina”遇到不顯示M符號的問題
方法一:切換到項目的工作目錄:svn upgrade
方法二:最近在使用git時,同步了遠程代碼倉庫到本地,然後把項目工程拖入進去,結果git無法對我的代碼沒有管理。解決辦法如下,希望能幫助到大家:
注意:當我們把代碼拖入進去後,需要把圖中框選的文件給刪掉。
'A' 新增
'D' 刪除
'M' 修改
'R' 替代
'C' 衝突
'I' 忽略
'?' 未受控
'!' 丟失,一般是將受控文件直接刪除導致
七、關於設置UITableViewCell分割線的問題
在ios7.0以前直接進行如下設置就行
tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine
在ios8.0以後增加了layoutMargins,它會使得即使設置了cell下劃線,也不會全部填充滿1、在viewDidLoad裏面如下設置:
if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
[self.tableView setSeparatorInset:UIEdgeInsetsZero];
}
if ([self.tableView respondsToSelector:@selector(setLayoutMargins:)]) {
[self.tableView setLayoutMargins:UIEdgeInsetsZero];
}