666666

1.isKindOfClass和isMemberOfClass之間的區(qū)別

isKindOfClass用來確定一個對象是否是一個類的實例,或者是該類祖先類的實例。
isMemberOfClass只能用來判斷前者,不能用來判斷后者。

2.懶加載

1.懶加載概念
懶加載——也稱為延遲加載,即在需要的時候才加載(效率低,占用內(nèi)存小)。所謂懶加載,寫的是其get方法.
注意:如果是懶加載的話則一定要注意先判斷是否已經(jīng)有了,如果沒有那么再去進行實例化
2.使用懶加載的好處:
(1)不必將創(chuàng)建對象的代碼全部寫在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 {
    //判斷是否已經(jīng)有了,若沒有,則進行實例化
    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.關(guān)于鍵盤

// 1.監(jiān)聽鍵盤的通知方法一
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
//  當鍵盤改變了frame(位置和尺寸)的時候調(diào)用
- (void)keyboardWillChangeFrame:(NSNotification *)note
{
    // 設(shè)置窗口的顏色
    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.執(zhí)行動畫
    [UIView animateWithDuration:duration animations:^{
        self.backgroundView.transform = CGAffineTransformMakeTranslation(0, transformY);
        self.view.transform = CGAffineTransformMakeTranslation(0, transformY);
    }];
}

// 2.監(jiān)聽鍵盤的通知方法二
[[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(@"結(jié)束編輯");
    CGRect frame = self.backgroundView.frame;
    frame.origin.y += 258;
    self.backgroundView.frame = frame;
}

// 3. 幾個常量
 UIKeyboardAnimationCurveUserInfoKey = 7;  // 動畫的執(zhí)行節(jié)奏(速度)
 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的簡單總結(jié)

SEL就是對方法的一種包裝。包裝的SEL類型數(shù)據(jù)它對應(yīng)相應(yīng)的方法地址,找到方法地址就可以調(diào)用方法。在內(nèi)存中每個類的方法都存儲在類對象中,每個方法都有一個與之對應(yīng)的SEL類型的數(shù)據(jù),根據(jù)一個SEL數(shù)據(jù)就可以找到對應(yīng)的方法地址,進而調(diào)用方法。

給自定義view添加點擊事件
@interface MyButton : UIView
@property(nonatomic,assign)id target;
@property(nonatomic,assign)SEL action;
// target:目標button執(zhí)行哪一個類的方法,對應(yīng)的目標就是那個類的對象
// action:動作,讓button具體做什么事,執(zhí)行的方法就是對應(yīng)的動作
- (void)addNewTarget:(id)target Action:(SEL)action;

- (void)addNewTarget:(id)target Action:(SEL)action{
    // 實現(xiàn)對應(yīng)的自定義方法,并且讓兩個屬性來保存對應(yīng)的目標和動作
    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小結(jié)

// 1.設(shè)置導航欄的標題
self.navigationItem.title = @"UINavigationBar使用總結(jié)";
// 2.通過barTintColor來設(shè)置背景色       
self.navigationController.navigationBar.barTintColor = [UIColor redColor];
// 3.設(shè)置返回按鈕的顏色
self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
// 4.設(shè)置返回按鈕的圖片
- (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側(cè)滑關(guān)閉失效的問題
    self.navigationController.interactivePopGestureRecognizer.delegate = (id)self;
}
UIImageRenderingModeAlwaysOriginal 表示總是用原圖渲染,如果不這么設(shè)置,返回按鈕將會顯示tintColor的顏色(默認為藍色)。UITabbarItem也存在同樣地問題。我們自己設(shè)置返回按鈕,會導致系統(tǒng)的側(cè)滑關(guān)閉效果失效。添加上面代碼中最后一句代碼即可修復。
// 5.隱藏導航欄底部的線條方法一
UINavigationBar *navigationBar = self.navigationController.navigationBar;
//設(shè)置透明的背景圖,便于識別底部線條有沒有被隱藏
[navigationBar setBackgroundImage:[[UIImage alloc] init]
                   forBarPosition:UIBarPositionAny
                       barMetrics:UIBarMetricsDefault];
// 此處使底部線條失效
[navigationBar setShadowImage:[UIImage new]];
// 6.隱藏導航欄底部的線條方法二
self.navigationController.navigationBar.clipsToBounds = YES;
// 7.設(shè)置導航欄底部線條顏色
UINavigationBar *navigationBar = self.navigationController.navigationBar;
[navigationBar setBackgroundImage:[[UIImage alloc] init]
                   forBarPosition:UIBarPositionAny
                       barMetrics:UIBarMetricsDefault];
// 此處使底部線條顏色為紅色, 調(diào)用如下顏色轉(zhuǎn)圖片的代碼
[navigationBar setShadowImage:[self imageWithColor:[UIColor redColor]]];

// 顏色轉(zhuǎn)圖片的代碼:
- (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加密算法多數(shù)用于驗證,比如說密碼匹配用的就是MD5加密后得到的數(shù)值。

import <CommonCrypto/CommonDigest.h>

-(NSString *)md5:(NSString *)str {
    const char *cStr = [str UTF8String];//轉(zhuǎn)換成utf-8
    unsigned char result[16];//開辟一個16字節(jié)(128位:md5加密出來就是128位/bit)的空間(一個字節(jié)=8字位=8個二進制數(shù))
    CC_MD5( cStr, strlen(cStr), result);
    /*
     extern unsigned char *CC_MD5(const void *data, CC_LONG len, unsigned char *md)官方封裝好的加密方法
     把cStr字符串轉(zhuǎn)換成了32位的16進制數(shù)列(這個過程不可逆轉(zhuǎn)) 存儲到了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
     */
}

常用博客

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 轉(zhuǎn)至元數(shù)據(jù)結(jié)尾創(chuàng)建: 董瀟偉,最新修改于: 十二月 23, 2016 轉(zhuǎn)至元數(shù)據(jù)起始第一章:isa和Class一....
    40c0490e5268閱讀 1,788評論 0 9
  • 把網(wǎng)上的一些結(jié)合自己面試時遇到的面試題總結(jié)了一下,以后有新的還會再加進來。 1. OC 的理解與特性 OC 作為一...
    AlaricMurray閱讀 2,615評論 0 20
  • 前言:面試筆試都是必考語法知識點。請認真復習和深入研究OC。 目錄:iOS-面試題-OC基礎(chǔ)篇 (1) - (84...
    麥穗0615閱讀 4,276評論 0 33
  • OC的理解與特性 OC作為一門面向?qū)ο蟮恼Z言,自然具有面向?qū)ο蟮恼Z言特性:封裝、繼承、多態(tài)。它既具有靜態(tài)語言的特性...
    小樓昨夜有風雨閱讀 586評論 0 0
  • OC的理解與特性 OC作為一門面向?qū)ο蟮恼Z言,自然具有面向?qū)ο蟮恼Z言特性:封裝、繼承、多態(tài)。它既具有靜態(tài)語言的特性...
    克魯?shù)吕?/span>閱讀 458評論 0 0