你好,歡迎來到IOS教程網

 Ios教程網 >> IOS訊息 >> 關於IOS >> IOS筆記匯集

IOS筆記匯集

編輯:關於IOS

1.iOS調用相冊和攝像頭

- (void)viewDidLoad {     [super viewDidLoad];     // Do any additional setup after loading the view.          UIImageView *imageView = [[UIImageView alloc] init];     imageView.frame = CGRectMake(0, 0, 80, 120);     imageView.backgroundColor = [UIColor greenColor];     imageView.tag = 101;          [self.view addSubview:imageView];          UIButton *button = [UIButton buttonWithType:UIButtonTypeRoundedRect];     button.frame = CGRectMake(0, 200, 100, 30);     ;     ;     [self.view addSubview:button];          UIButton *button2 = [UIButton buttonWithType:UIButtonTypeRoundedRect];     button2.frame = CGRectMake(0, 300, 100, 30);     [button2 setTitle:@"打開相機" forState:UIControlStateNormal];     [button2 addTarget:self action:@selector(openCamera) forControlEvents:UIControlEventTouchUpInside];     [self.view addSubview:button2]; }   // 打開相機 - (void)openCamera {     // UIImagePickerControllerCameraDeviceRear 後置攝像頭     // UIImagePickerControllerCameraDeviceFront 前置攝像頭     BOOL isCamera = [UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear];     if (!isCamera) {         NSLog(@"沒有攝像頭");         return ;     }          UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];     imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;     imagePicker.delegate = self;     // 編輯模式     imagePicker.allowsEditing = YES;          [self  presentViewController:imagePicker animated:YES completion:^{     }];        }     // 打開相冊 - (void)openPics {       UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init];     imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;     imagePicker.delegate = self;     [self  presentViewController:imagePicker animated:YES completion:^{     }];           }     // 選中照片   - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{     NSLog(@"%@", info);     UIImageView  *imageView = (UIImageView *)[self.view viewWithTag:101];     // UIImagePickerControllerOriginalImage 原始圖片     // UIImagePickerControllerEditedImage 編輯後圖片     UIImage *image = [info objectForKey:UIImagePickerControllerEditedImage];     imageView.image = image;     [picker dismissViewControllerAnimated:YES completion:NULL];      }       // 取消相冊 - (void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {     [picker dismissViewControllerAnimated:YES completion:NULL];   }     2.ASIHttpRequest框架使用 要使用ASIRequest必須添加5個動態庫,CFNetwork.framework、SystemConfigureation.framework、MobileCoreServices.framework、libz.dylib和libxml2.dylib     ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; [request setRequestMethod:@"GET"]; [request setTimeOutSeconds:60]; // 設置請求頭 // [request setRequestHeaders:<#(NSMutableDictionary *)#>] // 設置cookies // [request setRequestCookies:<#(NSMutableArray *)#>] // 發送同步請求 [request startSynchronous]; NSError *error = request.error; if (error == nil) {     NSData *data = request.responseData;     UIImage  *img = [UIImage imageWithData:data];     NSLog(@"%@", data);     self.image = img; } else {     NSLog(@"請求網絡出錯:%@", error); }     3.iOS Http請求異步請求 - (void)viewDidLoad {     [super viewDidLoad]; // Do any additional setup after loading the view.          _data = [[NSMutableData alloc] init];          // 組合一個搜索字符串     NSString *urlStr = [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@", @"php"];     NSURL *url = [NSURL URLWithString:urlStr];          NSURLRequest *request = [NSURLRequest requestWithURL:url];          //發起請求,定義代理     [NSURLConnection connectionWithRequest:request delegate:self];      }   // 分批返回數據 - (void)connection:(NSURLConnection *) connection didReceiveData:(NSData *)data {     [_data appendData:data];     NSLog(@"%@", _data); }   // 數據完全返回完畢 - (void)connectionDidFinishLoading:(NSURLConnection *)connection {     NSString *dataString =  [[NSString alloc] initWithData:_data encoding:NSUTF8StringEncoding];     NSLog(@"%@", dataString); }   4.iOS Http get 請求 // 組合一個搜索字符串     NSString *urlStr = [NSString stringWithFormat:@"http://www.baidu.com/s?wd=%@", @"php"];     // 字符串轉化為URL     NSURL *url = [NSURL URLWithString:urlStr];      //    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; //    [request setURL:url]; //    [request setHTTPMethod:@"POST"]; //    [request setTimeoutInterval:60]; //    [request setHTTPBody:_data]; //    [request setValue:@"ttt" forHTTPHeaderField:@"cookies"];         // url轉化為一個請求     NSURLRequest *request = [NSURLRequest requestWithURL:url];     // 狀態請求     NSURLResponse *response;     // 鏈接一個請求     NSData *resultData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:nil];     // 返回數據轉為字符串     NSData *dataString = [[NSString alloc] initWithData:resultData encoding:NSUTF8StringEncoding];     NSLog(@"%@", dataString); // 解析json吧   5.NSURL 基本方法     NSURL *url = [NSURL URLWithString:@"http://www.baidu.com/search?id=1"];     NSLog(@"scheme:%@", [url scheme]); //協議 http     NSLog(@"host:%@", [url host]);     //域名 www.baidu.com     NSLog(@"absoluteString:%@", [url absoluteString]); //完整的url字符串 http://www.baidu.com:8080/search?id=1     NSLog(@"relativePath: %@", [url relativePath]); //相對路徑 search     NSLog(@"port :%@", [url port]);  // 端口 8080     NSLog(@"path: %@", [url path]);  // 路徑 search     NSLog(@"pathComponents:%@", [url pathComponents]); // search     NSLog(@"Query:%@", [url query]);  //參數 id=1   6.軟件打開滑動導航 UIScrollView - (void)viewDidLoad {     [super viewDidLoad]; // Do any additional setup after loading the view.     _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, 340, 460)];     _scrollView.backgroundColor = [UIColor whiteColor];     _scrollView.delegate = self;     _scrollView.pagingEnabled = YES;     _scrollView.tag = INT_MAX;     _scrollView.showsHorizontalScrollIndicator = NO;          _scrollView.contentSize = CGSizeMake(340 * 4, 460);     [self.view addSubview:_scrollView];          int _x = 0;          for (int index = 0; index < 4; index++) {         UIImageView *imgScrollView = [[UIImageView alloc] initWithFrame:CGRectMake(0+_x, 0, 320, 460)];         imgScrollView.tag = index;         NSString *imgName = [NSString stringWithFormat:@"%d.JPG", index + 1];         imgScrollView.image = [UIImage imageNamed:imgName];         [_scrollView addSubview:imgScrollView];         _x += 340;              }          UIPageControl *pageControl = [[UIPageControl alloc] initWithFrame:CGRectMake(0, 180, 320, 50)];     pageControl.numberOfPages = 4;     pageControl.tag = 101;     [self.view addSubview:pageControl];     [pageControl release]; }   - (void) scrollViewDidEndDecelerating:(UIScrollView *)scrollView {     int current = scrollView.contentOffset.x / 320;     UIPageControl *pageControl = (UIPageControl *)[self.view viewWithTag:101];     pageControl.currentPage = current;      } 7.Objective-c 類初始化參數 #import <Foundation/Foundation.h>   @interface obj : NSObject { NSString *_name; } -(id) initWithName:(NSString *) name; -(void) setName:(NSString *)name; -(NSString *) getName;   @end   @implementation obj   -(NSString *) getName { return _name; }   -(void)setName:(NSString *) name { _name = name; }    // 初始化方法,帶參數 -(id) initWithName:(NSString *) name { // 調用父類init 生成類 self = [super init];   if (self) { // 執行自己的方法 [self setName:name]; } return self; } @end   int main(int argc, char *argv[]) { @autoreleasepool { obj *o = [[obj alloc] initWithName:@"wangdk"]; NSLog(@"name is %@", [o getName]);   } } 8.Objective-c 處理動態類型的方法 #import <Foundation/Foundation.h>   //-(BOOL) isKindOfClass:class // 對象是不是class或其子類成員 //-(BOOL) isMemberOfClass:class // 對象是不是class的成員 //-(BOOL) respondsToSelector:selector // 對象是否能夠相應selector所指定的方法 //+(BOOL) instancesRespondToSelector:selector // 指定對象實力是否能響應selector //+(BOOL) isSubclassOfClass:class // 對象是否指定類的子類 //-(id) performSwlector:selector // 應用selector指定的方法 //-(id) perforumSelector:selector widthObject:object // 應用selector指定方法傳參object   @interface obj : NSObject   @end   @implementation obj   @end   @interface obj2 : obj   -(void)setName; @end   @implementation obj2 -(void)setName {   } @end   int main(int argc, char *argv[]) { @autoreleasepool { obj *o = [[obj2 alloc] init]; // 判斷 o 是不是屬於obj 實例或子類實例 if ([o isKindOfClass: [obj class]] == YES) { NSLog(@" obj is a Kind of o class"); } // 判斷 o 是不是屬於 obj 實例 if ([o isMemberOfClass:[obj class]] == YES ) { NSLog(@"obj is member of class of o class"); }   // 判斷o是否可以響應setName方法 if ([o respondsToSelector:@selector(setName)] == YES) { NSLog(@" o respondsToSelector setName"); }   } }   9.IOS獲取當月天數 NSCalendar *calendar = [NSCalendar currentCalendar];          unsigned unitFlags = NSYearCalendarUnit | NSMonthCalendarUnit |  NSDayCalendarUnit;          NSDateComponents *components = [calendar components:unitFlags fromDate:[NSDate date]];          NSInteger iCurYear = [components year];  //當前的年份          NSInteger iCurMonth = [components month];  //當前的月份          NSInteger iCurDay = [components day];  // 當前的號數          NSString  *dateStr = nil;     NSMutableArray *arr = [NSMutableArray array];          for (NSInteger i = 1; i <= iCurDay; i++) {         dateStr  = [NSString stringWithFormat:@"%d-%d-%d", iCurYear, iCurMonth, i];         [arr addObject:dateStr];     }          _arrayList = [arr copy];   10.UITableDelgate 幾種代理方法 #pragma mark - TableView delegate - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {     if (indexPath.row == 0 && indexPath.section == 2) {         return 80; // 第三個section中第一行     }return 44; } // 設置行高   - (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {     if (section == 0) {         return 44;     }return 25; } // 設置section header的高度     - (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {     if (section == 12) {         return 80;     }return 50; } // 設置section footer的高度   /* - (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {     UIView *headerView = [[UIView alloc] initWithFrame:CGRectZero];     headerView.backgroundColor = [UIColor cyanColor];     return [headerView autorelease]; }*/ // 設置section自定義頭部視圖     - (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {     UIView *footerView = [[UIView alloc] initWithFrame:CGRectZero];     footerView.backgroundColor = [UIColor cyanColor];          UILabel *tipLabel = [[UILabel alloc] initWithFrame:CGRectMake(60, 0, 200, 30)];     tipLabel.numberOfLines = 0;     tipLabel.textAlignment = NSTextAlignmentCenter;     tipLabel.text = [NSString stringWithFormat:@"section footer %d", section+1];     [footerView addSubview:tipLabel];     [tipLabel release];          return [footerView autorelease]; } // 設置section自定義尾部視圖  
  1. 上一頁:
  2. 下一頁:
蘋果刷機越獄教程| IOS教程問題解答| IOS技巧綜合| IOS7技巧| IOS8教程
Copyright © Ios教程網 All Rights Reserved