1.isKindOfClass和isMemberOfClass之間的區別
isKindOfClass用來確定一個對象是否是一個類的實例,或者是該類祖先類的實例。
isMemberOfClass只能用來判斷前者,不能用來判斷后者。
2.懶加載
1.懶加載概念
懶加載——也稱為延遲加載,即在需要的時候才加載(效率低,占用內存小)。所謂懶加載,寫的是其get方法.
注意:如果是懶加載的話則一定要注意先判斷是否已經有了,如果沒有那么再去進行實例化
2.使用懶加載的好處:
(1)不必將創建對象的代碼全部寫在viewDidLoad方法中,代碼的可讀性更強
(2)每個控件的getter方法中分別負責各自的實例化處理,代碼彼此之間的獨立性強,松耦合
#import "MyViewController.h"
@interface MyViewController ()
@property (nonatomic, strong) UILabel *label;
@property (nonatomic, strong) NSArray *array;
@end
@implementation MyViewController
- (void)viewDidLoad{
[super viewDidLoad];
[self change];
}
// 先get再set
- (void)change {
NSDate *date = [NSDate date];
[self.label setText:[NSString stringWithFormat:@"%@",[date description]]];
}
// 延遲加載
- (UILabel *)label {
//判斷是否已經有了,若沒有,則進行實例化
if (!_label) {
_label=[[UILabel alloc]initWithFrame:CGRectMake(20, 10, 300, 30)];
[_label setTextAlignment:NSTextAlignmentCenter];
[self.view addSubview:_label];
}
return _label;
}
// array的get方法
- (NSArray *)array {
if (_array == nil) {
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
_array = [[NSArray alloc]initWithContentsOfFile:path];
}
return _array;
}
@end
3.關于鍵盤
// 1.監聽鍵盤的通知方法一
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
// 當鍵盤改變了frame(位置和尺寸)的時候調用
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
// 設置窗口的顏色
self.view.window.backgroundColor = self.tableView.backgroundColor;
// 0.取出鍵盤動畫的時間
CGFloat duration = [note.userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
// 1.取得鍵盤最后的frame
CGRect keyboardFrame = [note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
// 2.計算控制器的view需要平移的距離
CGFloat transformY = keyboardFrame.origin.y - self.view.frame.size.height;
// 3.執行動畫
[UIView animateWithDuration:duration animations:^{
self.backgroundView.transform = CGAffineTransformMakeTranslation(0, transformY);
self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
}];
}
// 2.監聽鍵盤的通知方法二
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(beginEditText:) name:UITextFieldTextDidBeginEditingNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endEditText:) name:UITextFieldTextDidEndEditingNotification object:nil];
- (void)beginEditText:(NSNotification *)notification{
NSLog(@"開始編輯");
CGRect frame = self.backgroundView.frame;
frame.origin.y -= 258;
self.backgroundView.frame = frame;
}
- (void)endEditText:(NSNotification *)notification{
NSLog(@"結束編輯");
CGRect frame = self.backgroundView.frame;
frame.origin.y += 258;
self.backgroundView.frame = frame;
}
// 3. 幾個常量
UIKeyboardAnimationCurveUserInfoKey = 7; // 動畫的執行節奏(速度)
UIKeyboardAnimationDurationUserInfoKey = "0.25"; // 鍵盤彈出\隱藏動畫所需要的時間
UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
UIKeyboardFrameChangedByUserInteraction = 0;
// 鍵盤彈出(隱藏)
UIKeyboardFrameBeginUserInfoKey // 鍵盤剛彈出(隱藏)那一刻的frame
UIKeyboardFrameEndUserInfoKey // 鍵盤彈出(隱藏)完畢后的frame
4.SEL的簡單總結
SEL就是對方法的一種包裝。包裝的SEL類型數據它對應相應的方法地址,找到方法地址就可以調用方法。在內存中每個類的方法都存儲在類對象中,每個方法都有一個與之對應的SEL類型的數據,根據一個SEL數據就可以找到對應的方法地址,進而調用方法。
給自定義view添加點擊事件
@interface MyButton : UIView
@property(nonatomic,assign)id target;
@property(nonatomic,assign)SEL action;
// target:目標button執行哪一個類的方法,對應的目標就是那個類的對象
// action:動作,讓button具體做什么事,執行的方法就是對應的動作
- (void)addNewTarget:(id)target Action:(SEL)action;
- (void)addNewTarget:(id)target Action:(SEL)action{
// 實現對應的自定義方法,并且讓兩個屬性來保存對應的目標和動作
self.action = action;
self.target = target;
}
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
// 類把她的方法,交給MyButton來完成
[self.target performSelector:self.action withObject:self];
}
初始化一個myButton
[myButton addNewTarget:self Action:@selector(click:)];
5.UINavigationBar小結
// 1.設置導航欄的標題
self.navigationItem.title = @"UINavigationBar使用總結";
// 2.通過barTintColor來設置背景色
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
// 3.設置返回按鈕的顏色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
// 4.設置返回按鈕的圖片
- (void)setBackButtonWithImage {
UIImage *leftButtonIcon = [[UIImage imageNamed:@"LeftButton_back_Icon"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:leftButtonIcon style:UIBarButtonItemStyleBordered target:self action:@selector(goToBack)];
// 修復navigationController側滑關閉失效的問題
self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
UIImageRenderingModeAlwaysOriginal 表示總是用原圖渲染,如果不這么設置,返回按鈕將會顯示tintColor的顏色(默認為藍色)。UITabbarItem也存在同樣地問題。我們自己設置返回按鈕,會導致系統的側滑關閉效果失效。添加上面代碼中最后一句代碼即可修復。
// 5.隱藏導航欄底部的線條方法一
UINavigationBar *navigationBar = self.navigationController.navigationBar;
//設置透明的背景圖,便于識別底部線條有沒有被隱藏
[navigationBar setBackgroundImage:[[UIImage alloc] init]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
// 此處使底部線條失效
[navigationBar setShadowImage:[UIImage new]];
// 6.隱藏導航欄底部的線條方法二
self.navigationController.navigationBar.clipsToBounds = YES;
// 7.設置導航欄底部線條顏色
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar setBackgroundImage:[[UIImage alloc] init]
forBarPosition:UIBarPositionAny
barMetrics:UIBarMetricsDefault];
// 此處使底部線條顏色為紅色, 調用如下顏色轉圖片的代碼
[navigationBar setShadowImage:[self imageWithColor:[UIColor redColor]]];
// 顏色轉圖片的代碼:
- (UIImage *)imageWithColor:(UIColor *)color
{
CGRect rect = CGRectMake(0.0f, 0.0f, 1.0f, 1.0f);
UIGraphicsBeginImageContext(rect.size);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextSetFillColorWithColor(context, [color CGColor]);
CGContextFillRect(context, rect);
UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return image;
}
6.將字符串進行MD5加密,返回加密后的字符串。MD5加密算法多數用于驗證,比如說密碼匹配用的就是MD5加密后得到的數值。
import <CommonCrypto/CommonDigest.h>
-(NSString *)md5:(NSString *)str {
const char *cStr = [str UTF8String];//轉換成utf-8
unsigned char result[16];//開辟一個16字節(128位:md5加密出來就是128位/bit)的空間(一個字節=8字位=8個二進制數)
CC_MD5( cStr, strlen(cStr), result);
/*
extern unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md)官方封裝好的加密方法
把cStr字符串轉換成了32位的16進制數列(這個過程不可逆轉) 存儲到了result這個空間中
*/
return [NSString stringWithFormat:
@"%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X",
result[0], result[1], result[2], result[3],
result[4], result[5], result[6], result[7],
result[8], result[9], result[10], result[11],
result[12], result[13], result[14], result[15]
];
/*
x表示十六進制,%02X 意思是不足兩位將用0補齊,如果多余兩位則不影響
NSLog("%02X", 0x888); //888
NSLog("%02X", 0x4); //04
*/
}
常用博客