1. 不可變數組 轉變為可變數組
聲明執行個體變數的數組 必須記得實現
對於遍曆數組找到對象後 如果還需要尋找 記得先結束 再尋找(return/break) NSArray * arr = @[@"人在囧途",@"煎餅俠",@"西遊記",];
NSMutableArray * arr = [NSMutableArray arrayWithArray:arr]; 在數組中取資料的時候 需要通過尾碼 將數組中的對象轉化為數字類
p11. age = [newArr[1] intValue ];
2.擷取字串長度
NSString * str = nameLabel .text;
CGSize size = [str sizeWithAttributes :@{ NSFontAttributeName : [UIFont systemFontOfSize:17]}];
此時即可得到 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. 如果父視圖可以互動 那麼放在上邊的子視圖的可互動性由自己決定
關鍵詞 : userInteractionEnabled
image . 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 = [UIImage imageNamed:@"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 . navigationController popViewControllerAnimated :