一個iOS開發者必須掌握的66個知識點,你掌握了多少?
對於遍歷陣列找到物件後 如果還需要查詢 記得先結束 再查詢(return/break)
NSArray * arr = @[@"人在囧途",@"煎餅俠",@"西遊記",];NSMutableArray * arr = [NSMutableArrayarrayWithArray:arr];
在陣列中取資料的時候 需要通過後綴 將陣列中的物件轉化為數字類p11.age= [newArr[1]intValue];2.獲取字串長度NSString * str = nameLabel .text;
CGSize
此時即可得到size .width(字串的寬 即 字串長度)
3.將單獨的某個檢視上的檢視控制器的導航條隱藏
-(void)viewDidAppear:(BOOL)animated{
[super viewDidAppear:animated == NO];
[self .navigationController setNavigationBarHidden:YES];
}
4. 邊帽法 (拉伸圖片) 將某一畫素點兒不斷地複製 其他畫素點不變 拉伸之後不會是圖片變形//第一種 [[UIImageView alloc] init].image = [[UIImage imageNamed:@"bubble.png"] stretchableImageWithLeftCapWidth:20 topCapHeight:20]; //第二種 UIImage * image =[UIImage imageNamed:@"7.jpg"]; [image resizableImageWithCapInsets:UIEdgeInsetsMake(0, image.size.width, 0, 0)]; [[UIImageView alloc] init].image = image;
5. 監聽系統傳送的通知
// 監聽鍵盤frame發生變化的通知 並可以通過鍵盤的屬性獲得物件
[[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(KeyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
- (void)KeyboardWillChangeFrame:(NSNotification *)noti {
// UIKeyboardAnimationCurveUserInfoKey = 7;
// UIKeyboardAnimationDurationUserInfoKey = "0.25";
// UIKeyboardBoundsUserInfoKey = "NSRect: {{0, 0}, {320, 216}}";
// UIKeyboardCenterBeginUserInfoKey = "NSPoint: {160, 588}";
// UIKeyboardCenterEndUserInfoKey = "NSPoint: {160, 372}";
// UIKeyboardFrameBeginUserInfoKey = "NSRect: {{0, 480}, {320, 216}}";
// UIKeyboardFrameChangedByUserInteraction = 0;
// UIKeyboardFrameEndUserInfoKey = "NSRect: {{0, 264}, {320, 216}}";
//一旦鍵盤發生改變的時候 _inputView 和 _tableView 的座標都要發生改變
//通過字典 獲取物件
NSDictionary * dic = noti .userInfo;
//獲取動畫時長
float time = [[dic objectForKey:UIKeyboardAnimationDurationUserInfoKey] floatValue];
//獲取動畫速率
int curve = [[dic objectForKey:UIKeyboardAnimationCurveUserInfoKey] intValue];
//獲取鍵盤座標
CGRect rect = [[dic objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue];
}
6.通過字串繪製矩形 MAXFLOAT 無限
NSString * str = _chatArr[indexPath .row];
CGRect rect = [str boundingRectWithSize:CGSizeMake(200, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName: [UIFont systemFontOfSize:15]} context:nil];
7.檢視層級切換關鍵點:
[self .window exchangeSubviewAtIndex:0 withSubviewAtIndex:1];
1.如果父檢視不可互動 那麼放在其上邊的子檢視也不可互動
2. 如果父檢視可以互動 那麼放在上邊的子檢視的可互動性由自己決定
關鍵詞 :userInteractionEnabledimage .userInteractionEnabled=YES;
8.二進位制資料轉變型別
字串轉變為二進位制資料
NSString * str = @"girls";
NSData * data = [str dataUsingEncoding:NSUTF8StringEncoding];二進位制資料轉變為字串
NSString * str2 =[[NSString alloc]initWithData:data1 encoding:NSUTF8StringEncoding];將圖片轉變為二進位制資料
1.需要獲得圖片路徑
NSString * imageViewPath =[[NSBundle mainBundle]pathForResource:@"18" ofType:@"jpg"];
NSData * data = [[NSData alloc]initWithContentsOfFile:imageViewPath];
2.直接將新增在工程中的圖片轉化為二進位制資料型別
UIImage* image = [UIImageimageNamed:@"20.jpg"];
NSData* data = UIImageJPEGRepresentation(image,1);將轉變為二進位制資料的圖片轉變回圖片
方法1 可以直接呼叫 從路徑中取出圖片
UIImage * image = [UIImage imageWithContentsOfFile:dataPath];
方法2 : 先將路徑中的二進位制資料取出 然後 通過ImageWithData 屬性轉變為圖片型別
NSData * data = [NSData dataWithContentsOfFile:[self getFilePath:@"data.plist"]];
UIImage * image =[UIImage imageWithData:data]; 9.如何刪除一個檢視上的所有子檢視 所有程式碼:獲取檢視上存放的所有子檢視 遍歷陣列 找到所有物件 找到 views
for (UIView * v in self.view.subviews) {
[v removeFromSuperview];
}
10.將某個檢視放在最前邊 [self .view bringSubviewToFront:_tableView];11.頁面跳轉(需要找window)
//找window
方法1:
UIWindow * window =[[[UIApplication sharedApplication]delegate]window];
window .rootViewController = aViewController;
方法2:UIApplication * app = [UIApplication sharedApplication];
AppDelegate * delegate = app .delegate;
UIWindow * window = delegate .window ;
window .rootViewController = aViewController;
1.頁面翻轉
[UIView beginAnimations:nil context:nil];
[UIView setAnimationDuration:1];
[UIView setAnimationTransition:UIViewAnimationTransitionCurlUp forView:window cache:YES];
[UIView commitAnimations];
2. 模態彈出 (present 展示 dismiss 消失 )
3. 導航控制器 (push 壓棧 pop 出棧 )
//通過導航控制器控制檢視切換的時候1. 返回上一介面:
[self.navigationControllerpopViewControllerAnimated:YES];2. 返回根檢視控制器:
[self.navigationControllerpopToRootViewControllerAnimated:YES];3.返回指定檢視:
//首先獲取目前所有的檢視控制器物件NSArray* arr = self.navigationController.viewControllers;
//從陣列中找到需要返回的根檢視FirestViewController* first = arr[0];
//返回指定檢視:[self.navigationControllerpopToViewController:firstanimated:YES];
12.將資料庫檔案拷貝到沙盒中//首先需要獲取資料庫檔案的路徑 還有資料夾路徑
NSString * sourePath = [[NSBundle mainBundle]pathForResource:@"database" ofType:@"sqlite"];
if (![[NSFileManager defaultManager] fileExistsAtPath:@"檔案路徑"]) {
NSError * error = nil;
if ( [[NSFileManager defaultManager]copyItemAtPath:sourePath toPath:@"檔案路徑" error:&error]) {
NSLog(@"copy成功");
}
}
13:直接進入網頁
//網頁載入 :
// 建立url NSURL: NSObject 用來表示資源在網際網路的位置
NSURL * url = [NSURL URLWithString:@"http://www.baidu.com"];
//建立一個請求
NSURLRequest *request = [NSURLRequest requestWithURL:url];
//載入請求
[webView loadRequest:request];
[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@"http//www.baidu.com"]];
14:將結構體轉換成字串再輸出
NSLog(@"-------%@",NSStringFromCGRect(rect));
15:角度轉弧度
可以定義為巨集#define ANGLE_2_HUDU(X) (X)*M_PI/180.0
計算圓形軌跡上控制元件的中心點座標時中心點 圓的半徑 角度轉弧度 角度值floatx = 160 + 100 *cos(ANGLE_2_HUDU(btn.angle));
floaty = 240 + 100 *sin(ANGLE_2_HUDU(btn .angle));
//x = 中心點x + 半徑 * [email protected]
//y = 中心點y + 半徑 *[email protected]16:交叉匯入
//為了防止交叉匯入 @class 檔案
//只是告訴了編譯器有這個類 但是該類的.h檔案中有什麼東西 編譯器是不知道的 當真正需要使用這個類的時候 還必須在.m 檔案中導入#import "TwoViewController.h”這個標頭檔案
17: center + bounds = frame ; //center(設定中心點) + bounds(設定寬.高) = frameimage .center = CGPointMake(160, 120);
//bounds (x,y ,width ,height ), 給bounds設定x和y是沒有作用的
image .bounds = CGRectMake(0, 0, 160, 120);
18:查詢父檢視
NSLog(@"==%@",image.superview);檢視view的子檢視
NSLog(@"==%@",self .window .subviews);
19.重新新增xib檔案
如果建立的xib 檔案的名字不同 需要在入口方法中呼叫一下方法ViewController * vc = [[ViewController alloc]initWithNibName:@"Empty" bundle:nil];
20.螢幕獲取觸控物件
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
//獲取觸控物件
UITouch * touch =[touches anyObject];
//獲取觸控物件 在某一檢視上的一個點
CGPoint point = [touch locationInView:self .view];
//把結構體轉換為字串輸出
NSLog(@"---%@",NSStringFromCGPoint(point));
//CGRectContainsPoint 作用 : 判斷CGRect 資料是否包含一個點 point 如果包含這個點 函式值就是1
//類似屬性 CGRectIntersectsRect(CGRect rect1, CGRect rect2) 判斷兩個座標是否有交集
//NO1. 觸控物件座標 NO2.結構體
if (CGRectContainsPoint(self.view.frame, point)) {
self.view.center = point;
}
}
21:判斷物件之間聯絡常用的屬性
1.判斷某個物件座標時候包含某個點
CGRectContainsPoint(self.frame.frame, point)2. 判斷兩個座標是否有交集
CGRectIntersectsRect(rect1, rect2)3.判斷兩個物件時候相等的時候
isEqualToString:
4. 判斷字串型別資料長度的時候 需要呼叫length 22.獲取系統當前時間NSDate * date = [NSDate date];
//時間格式 年yyyy 月 MM 日dd 小時 HH 分鐘 mm 秒 ss
NSDateFormatter * formatter = [[NSDateFormatter alloc]init];
[formatter setDateFormat:@"yy-MM-dd HH-mm-ss"];
NSString *str = [formatter stringFromDate:date];
NSLog(@"-=-=-=-=-=-=-===%@",str);
23. 分割(返回值型別 - 陣列) . 拼接字串
//分割:
NSArray * arr =[str componentsSeparatedByString:@" "];//字串之間用什麼隔開 分割時 在@“” 中就呼叫什麼 (eg: | 空格 等);
//拼接:[NSString stringWithString:@""]
[NSString stringWithFormat:@""]
去掉字串中間的空格return [self stringByReplacingOccurrencesOfString:@" " withString:@""];
去掉字串的換行符
str =[str stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];24 .定時器 .延遲載入
//定時器開啟的時候 為防止點選按鈕時開啟多個定時器造成混亂 所以在開啟定時器的時候可以先判斷時候有開啟定時器
方法1:
static BOOL isOk;
if (isOk == NO) {
NSTimer * timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
//開啟定時器後 如果想要定時器立即開啟 可以呼叫
[timer fire];
isOk = YES;
}
方法2 :
if (_timer) {
return;
}
_timer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
//關閉定時器時 記得立即將其指為空
[_timer invalidate];
_timer = nil;
延時載入
//定時器繫結方法的時候 只能講自身傳過來
_timer = [NSTimer scheduledTimerWithTimeInterval:0.05 target:self selector:@selector(onTimer:) userInfo: button repeats:YES];
-(void)onTimer: (NSTimer *)aTimer{
//通過userInfo 獲取點選的按鈕
UIButton * clickBtn = aTimer.userInfo;
}
//延遲載入 要點 需要呼叫performSelector
[self performSelector:@selector() withObject:nil afterDelay:5];
25 .文字框結束編輯的時候 讓鍵盤下去-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event{
[self .window endEditing:YES];
}
26:向相簿中儲存圖片
UIImageWriteToSavedPhotosAlbum([UIImage imageNamed:@"20.jpg"], self, @selector(image:didFinishSavingWithError:contextInfo:), NULL);
//必要實現的協議方法, 不然會崩潰
- (void)image:(UIImage *)image didFinishSavingWithError:(NSError *)error contextInfo:(void *)contextInfo {
}
27.輸出當前所在的方法名:NSLog(@"-----%s---", __func__);
28.三目運算子
條件 ?(執行操作) : 其它操作
number = i <= 8 ?(i + 1) : 0 解釋 : 當i 小於等於8 時 執行 i+1 的操作 否則 執行冒號後面的操作 即 i = 0;29.在陣列中隨機取出資料
1) 首先先在陣列中隨機出一個索引
int random = arc4random()%numberArray .count;
2) 將取出的資料轉化為想要的型別
int number = [numberArray [random] intvalue];
3) 將取出的資料在陣列中刪除 以防止隨機重複
[numberArray removeObjectAtIndex : random];30.檢視修剪 .透明度 隱藏檢視 移除檢視 :
clip 修剪 bounds 邊界1.是否對檢視修剪
bigView .clipsToBounds=YES;2.設定圓角半徑
bigView .layer.cornerRadius= 90;
bigView.layer.masksToBounds=YES;
3.透明度:_view .alpha = 0;
4.移除檢視 :
[_view removeFromSuperview]; 31: 字串拼接輸出控制元件屬性值NSLog(@" = %@",NSStringFromCGRect(nav.navigationBar.frame));32:第一響應:
1.開啟程式時 文字框處於編輯狀態 即 第一響應
成為第一響應 [_textfieldbecomeFirstResponder];
失去第一響應[_textfield resignFirstResponder];2.在tocubegin協議方法中呼叫
[self.view endEditing: YES] 33 .導航控制器相關控制元件內容1.設定導航條的背景顏色
錯誤做法: nav .navigationBar.backgroundColor= [UIColorgrayColor];
正確做法: nav .navigationBar.barTintColor= [UIColorgrayColor];2.設定導航條標題
self.title=@"導航條";
3. 設定左右欄按鈕項 (記得按鈕繫結方法的實現)
UIBarButtonItem * lifeItem = [[UIBarButtonItem alloc]initWithTitle:@"儲存" style:UIBarButtonItemStylePlain target:self action:(@selector(baoCunBtnClick:))];
self .navigationItem .leftBarButtonItem = lifeItem;
UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithTitle:@"新增分組" style:UIBarButtonItemStylePlain target:self action:(@selector(addGrounpBtnClick:))];
self .navigationItem .rightBarButtonItem = rightItem;
4.隱藏導航條 :
//注意: 隱藏導航條的時候 不能混著用 怎麼讓導航條隱藏的 就怎麼讓導航條出現 :方法1:
[self.navigationControllersetNavigationBarHidden:YES];
[self.navigationControllersetNavigationBarHidden:YESanimated:YES];
方法2:
self.navigationController.navigationBarHidden=YES;
self.