IOS開發基礎知識--片段19,ios基礎知識--片段
1:鍵盤事件順序
UIKeyboardWillShowNotification // 鍵盤顯示之前UIKeyboardDidShowNotification // 鍵盤顯示完成後UIKeyboardWillHideNotification // 鍵盤隱藏之前UIKeyboardDidHideNotification // 鍵盤訊息之後UIKeyboardWillChangeFrameNotification // 鍵盤大小改變之前UIKeyboardDidChangeFrameNotification // 鍵盤大小改變之後
2:程式報-[__NSCFDictionary xxx]: unrecognized selector sen
原因就是對象是一個字典,所以不能用點文法,postList.slots錯誤解決辦法:[postList valueForKey:@"slots"],使用這種文法 valueForKey 就可以了。
3:UIScreen學習記錄
UIScreen對象包含了整個螢幕的邊界矩形。當構造應用的使用者介面介面時,你應該使用該對象的屬性來獲得推薦的矩形大小,用以構造你的程式視窗。CGRect bound = [[UIScreen mainScreen] bounds]; // 返回的是帶有狀態列的Rect CGRect frame = [[UIScreen mainScreen] applicationFrame]; // 返回的是不帶有狀態列的Rect float scale = [[UIScreen mainScreen] scale]; // 得到裝置的自然解析度 對於scale屬性需要做進一步的說明: 以前的iphone 裝置螢幕解析度都是320*480,後來apple 在iPhone 4中採用了名為Retina的顯示技術,iPhone 4採用了960x640像素解析度的顯示螢幕。由於螢幕大小沒有變,還是3.5英寸,解析度的提升將iPhone 4的顯示解析度提升至iPhone 3GS的四倍,每英寸的面積裡有326個像素。scale屬性的值有兩個:scale = 1; 的時候是代表當前裝置是320*480的解析度(就是iphone4之前的裝置)scale = 2; 的時候是代表解析度為640*960的解析度// 判斷螢幕類型,普通還是視網膜 float scale = [[UIScreen mainScreen] scale]; if (scale == 1) { bIsRetina = NO; NSLog(@"普通螢幕"); }else if (scale == 2) { bIsRetina = YES; NSLog(@"視網膜螢幕"); }else{ NSLog(@"unknow screen mode !"); }
4:IOS開發NSBundle對象使用詳解
bundle是一個目錄,其中包含了程式會使用到的資源. 這些資源套件含了像,聲音,編譯好的代碼,nib檔案(使用者也會把bundle稱為plug-in). 對應bundle,cocoa提供了類NSBundle.我們的程式是一個bundle. 在Finder中,一個應用程式看上去和其他檔案沒有什麼區別. 但是實際上它是一個包含了nib檔案,編譯代碼,以及其他資源的目錄. 我們把這個目錄叫做程式的main bundlebundle中的有些資源可以本地化.例如,對於foo.nib,我們可以有兩個版本: 一個針對英語使用者,一個針對法語使用者. 在bundle中就會有兩個子目錄:English.lproj和French.lproj,我們把各自版本的foo.nib檔案放到其中. 當程式需要載入foo.nib檔案時,bundle會自動根據所設定的語言來載入.通過使用下面的方法得到程式的main bundleNSBundle *myBundle = [NSBundle mainBundle];一般我們通過這種方法來得到bundle.如果你需要其他目錄的資源,可以指定路徑來取得bundleNSBundle *goodBundle;goodBundle = [NSBundle bundleWithPath:@"~/.myApp/Good.bundle"];一旦我們有了NSBundle 對象,那麼就可以訪問其中的資源了// Extension is optionalNSString *path = [goodBundle pathForImageResource:@"Mom"];NSImage *momPhoto = [[NSImage alloc] initWithContentsOfFile:path];bundle中可以包含一個庫. 如果我們從庫得到一個class, bundle會串連庫,並尋找該類:Class newClass = [goodBundle classNamed:@"Rover"];id newInstance = [[newClass alloc] init];如果不知到class名,也可以通過尋找主要類來取得Class aClass = [goodBundle principalClass];id anInstance = [[aClass alloc] init];可以看到, NSBundle有很多的用途.在這章中, NSBundle負責(在後台)載入nib檔案. 我們也可以不通過NSWindowController來載入nib檔案, 直接使用NSBundle:BOOL successful = [NSBundle loadNibNamed:@"About" owner:someObject];注意噢, 我們指定了一個對象someObject作為nib的File”s Owner擷取XML檔案NSString *filePath = [[NSBundle mainBundle] pathForResouse:@"re" ofType:@"xml"];NSData *data = [[NSData alloc] initWithContentsOfFile:filePath]; 擷取屬性列表NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"ViewControllers" ofType:@"plist"]];
5:單位換算,PX換算成磅
用ps設計ios App字型以像素為單位,而ios開發人員寫代碼的時候是以磅為單位,請問像素與磅之間的換算30px轉成磅為單位=22磅=二號磅=(像素/96)*72 =(30/96)*72 =22.5磅中文字型大小VS英文字型大小(磅)VS像素值的對應關係:八號=5磅(5pt) ==(5/72)*96=6.67 =6px(像素)七號=5.5磅 ==(5.5/72)*96=7.3 =7px(像素)小六=6.5磅 ==(6.5/72)*96=8.67 =8px(像素)六號=7.5磅 ==(7.5/72)*96=10px(像素)小五=9磅 ==(9/72)*96=12px(像素)五號=10.5磅 ==(10.5/72)*96=14px(像素)小四=12磅 ==(12/72)*96=16px(像素)四號=14磅 ==(14/72)*96=18.67 =18px(像素)小三=15磅 ==(15/72)*96=20px(像素)三號=16磅 ==(16/72)*96=21.3 =21px(像素)小二=18磅 ==(18/72)*96=24px(像素)二號=22磅 ==(22/72)*96=29.3 =29px(像素)小一=24磅 ==(24/72)*96=32px(像素)一號=26磅 ==(26/72)*96=34.67 =34px(像素)
6:UIButton一些細節問題
// 能夠定義的button類型有以下6種,// typedef enum {// UIButtonTypeCustom = 0, 自訂風格// UIButtonTypeRoundedRect, 圓角矩形 // UIButtonTypeDetailDisclosure, 藍色小箭頭按鈕,主要做詳細說明用// UIButtonTypeInfoLight, 亮色驚嘆號// UIButtonTypeInfoDark, 暗色驚嘆號// UIButtonTypeContactAdd, 十字加號按鈕// } UIButtonType;/* forState: 這個參數的作用是定義按鈕的文字或圖片在何種狀態下才會顯現*///以下是幾種狀態// enum {// UIControlStateNormal = 0, 常規狀態顯現 // UIControlStateHighlighted = 1 << 0, 高亮狀態顯現 // UIControlStateDisabled = 1 << 1, 禁用的狀態才會顯現// UIControlStateSelected = 1 << 2, 選中狀態 // UIControlStateApplication = 0x00FF0000, 當應用程式標誌時 // UIControlStateReserved = 0xFF000000 為內部架構預留,可以不管他 // };/** 預設情況下,當按鈕高亮的情況下,映像的顏色會被畫深一點,如果這下面的這個屬性設定為no,* 那麼可以去掉這個功能*/button1.adjustsImageWhenHighlighted = NO;/*跟上面的情況一樣,預設情況下,當按鈕禁用的時候,映像會被畫得深一點,設定NO可以取消設定*/button1.adjustsImageWhenDisabled = NO;/* 下面的這個屬性設定為yes的狀態下,按鈕按下會發光*/button1.showsTouchWhenHighlighted = YES;長按事件執行個體:UIButton *aBtn=[UIButton buttonWithType:UIButtonTypeCustom]; [aBtn setFrame:CGRectMake(40, 100, 60, 60)]; [aBtn setBackgroundImage:[UIImage imageNamed:@"111.png"]forState:UIControlStateNormal]; //button點擊事件 [aBtn addTarget:self action:@selector(btnShort:)forControlEvents:UIControlEventTouchUpInside]; //button長按事件 UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:selfaction:@selector(btnLong:)]; longPress.minimumPressDuration = 0.8; //定義按的時間 [aBtn addGestureRecognizer:longPress];-(void)btnLong:(UILongPressGestureRecognizer*)gestureRecognizer{ if ([gestureRecognizer state] == UIGestureRecognizerStateBegan) { NSLog(@"長按事件"); UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"訊息" message:@"確定刪除該模式嗎?" delegate:selfcancelButtonTitle:@"取消" otherButtonTitles:@"刪除", nil]; [alert show]; }}
7:UIApplication知識點
UIApplication對象,這個對象在iOS中是一個單例,我們通過[UIApplication sharedApplication]獲得,設定顯示訊息數,顯示在應用程式圖示右上方,[UIApplication sharedApplication].applicationIconBadgeNumber=9;可以通過獲得NSIntger x=[UIApplication sharedApplication].applicationIconBadgeNumber; 防止螢幕睡眠:[UIApplication sharedApplication].idleTimerDisabled=YES; 設定狀態列樣式在app delegate中:[UIApplication sharedApplication].statusBarStyle = UIStatusBarStyleLightContent;
8:一個倒計時的功能代碼
#import "ViewController.h"@interface ViewController (){ NSTimer *timer; NSInteger nowSeconds;}@end@implementation ViewController- (void)viewDidLoad { [super viewDidLoad]; nowSeconds = 30 * 100; //(定義的30秒進行倒計) timer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timerAction) userInfo:nil repeats:YES]; [[NSRunLoop currentRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];}- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated.}- (void)timerAction{ if(nowSeconds<0) { [timer invalidate]; timer = nil; return; } nowSeconds--; if(nowSeconds<=0) { self.mylable.text = @"正在揭曉..."; return; } int m = (int)nowSeconds / 6000; int s = (int)(nowSeconds/100) - m*60; NSString* f1 = s > 9 ? [NSString stringWithFormat:@"%d",s] : [@"0" stringByAppendingFormat:@"%d",s]; int ms = nowSeconds % 100; NSString* f2 = ms > 9 ? [NSString stringWithFormat:@"%d",ms] : [@"0" stringByAppendingFormat:@"%d",ms]; self.mylable.text = [NSString stringWithFormat:@"0%d:%@:%@",m,f1,f2];}@end
9:BlocksKit外掛程式運用
引入#import <BlocksKit/BlocksKit.h>#import <BlocksKit/BlocksKit+UIKit.h>常見的一些blocks: //視圖 UIView *bcView=[[UIView alloc]init]; bcView.backgroundColor=[UIColor redColor]; bcView.frame=CGRectMake(30, 40, 50, 20); [bcView bk_whenTapped:^{ NSLog(@"單擊響應"); }]; [bcView bk_whenDoubleTapped:^{ NSLog(@"雙擊響應"); }]; [bcView bk_whenTouches:1 tapped:3 handler:^{ NSLog(@"三擊響應"); }]; [self.view addSubview:bcView]; //Control btn=[[UIButton alloc]initWithFrame:CGRectMake(70, 70, 50, 50)]; [btn setTitle:@"Save" forState:UIControlStateNormal]; btn.backgroundColor=[UIColor grayColor]; [btn bk_addEventHandler:^(id sender) { NSLog(@"UIControlEventTouchUpInside響應"); //判斷跟移除響應 [self getUIButtonEventHandler]; } forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:btn]; //UIAlertView UIAlertView *alertView=[UIAlertView bk_showAlertViewWithTitle:@"彈出窗效果" message:@"你好,請選擇" cancelButtonTitle:@"取消" otherButtonTitles:@[@"確定",@"不想選"] handler:^(UIAlertView *alertView, NSInteger buttonIndex) { if (buttonIndex==0) { NSLog(@"你選擇第一個"); } else if(buttonIndex==1) { NSLog(@"你選擇第二個"); } else { NSLog(@"選擇其它個"); } }]; [alertView show]; __block NSInteger total=0; UIAlertView *otherAlertView=[[UIAlertView alloc]bk_initWithTitle:@"動態增加" message:@"是否是要增加"]; NSInteger index1 = [otherAlertView bk_addButtonWithTitle:@"確定" handler:^{ total++; NSLog(@"%ld",total); }]; [otherAlertView.bk_dynamicDelegate alertView:otherAlertView clickedButtonAtIndex:index1]; [otherAlertView show]; UIAlertView *showAlert=[[UIAlertView alloc] initWithTitle:@"系統內建的alertview" message:@"顯示資訊" delegate:self cancelButtonTitle:@"取消" otherButtonTitles: nil]; showAlert.bk_cancelBlock=^() { NSLog(@"取消響應"); }; showAlert.bk_willShowBlock = ^(UIAlertView *view) { NSLog(@"顯示之前響應");}; showAlert.bk_didShowBlock = ^(UIAlertView *view) { NSLog(@"顯示出彈出窗響應"); }; [showAlert show]; //UIActionSheet UIActionSheet *myactionSheet=[[UIActionSheet alloc]initWithTitle:@"顯示資訊" delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:@"相關內容的顯示" otherButtonTitles:nil]; myactionSheet.bk_cancelBlock=^() { NSLog(@"選擇取消actionSheet"); }; [myactionSheet showInView:self.view]; //UITextField UITextField *myTextField = [[UITextField alloc] init]; myTextField.backgroundColor=[UIColor yellowColor]; myTextField.frame=CGRectMake(10, 130, 80, 20); myTextField.bk_shouldBeginEditingBlock=^(UITextField *textField) { NSLog(@"shouldBeginEditingBlock"); return YES; }; myTextField.bk_shouldBeginEditingBlock = ^(UITextField *textField) { NSLog(@"shouldBeginEditingBlock"); return YES; }; myTextField.bk_didBeginEditingBlock = ^(UITextField *textField) { NSLog(@"didBeginEditingBlock"); }; myTextField.bk_shouldEndEditingBlock = ^(UITextField *textField) { NSLog(@"shouldEndEditingBlock"); return YES; }; myTextField.bk_didEndEditingBlock = ^(UITextField *textField) { NSLog(@"didEndEditingBlock "); }; myTextField.bk_shouldChangeCharactersInRangeWithReplacementStringBlock = ^(UITextField *textField, NSRange range, NSString *replacement) { NSLog(@"shouldChangeCharactersInRangeWithReplacementStringBlock"); return YES; }; myTextField.bk_shouldClearBlock = ^(UITextField *textField) { NSLog(@"shouldClearBlock"); return YES; }; myTextField.bk_shouldReturnBlock = ^(UITextField *textField) { NSLog(@"shouldReturnBlock"); return YES; }; [self.view addSubview:myTextField];