IOS基础知识记录(转载)

(转载)http://www.cnblogs.com/aimeng/archive/2013/08/05/3238358.html

1.通过UISwitch开关隐藏一个btn, 通过监听value change事件

self.btn.hidden = ![sender isOn];

2.获取UISegmentedControl的标题

[segment titleForSegmentAtIndex: [segment selectedSegmentIndex]]

3.获取0~~~~100000随机数int类型 

rand()*100000;

4.加载webView页面 

   NSString * urlString = [[NSString alloc] initWithFormat:@"http://www.google.com“];
   NSURL *url = [[NSURL alloc] initWithString: urlString];
   [view loadRequest:[NSURLRequest requestWithURL: url];

1.提醒用户信息, 铃声提醒或者震动提示 

 SystemSoundID soundID;
    NSString *url = [[NSBundle mainBundle] pathForResource: @"sound" ofType: @"wav"];
    
    AudioServicesCreateSystemSoundID((__bridge CFURLRef)[NSURL fileURLWithPath:url], &soundID);
    AudioServicesPlaySystemSound(soundID);
   //AudioServicesPlayAlertSound(soundID)

AudioServicesPlaySystemSound如果系统静音, 则用户提示就没有什么效果
AudioServicesPlayAlertSound如果静音会震动,否则声音提示
AudioServicesPlaySystemSound(kSystemSoundID_Vibrate);单一的震动

IOS基础知识记录三(modal模态切换)

1.模态切换过渡有几种类型
  ①. cover vertical从下向上移动,覆盖旧scene
  ②. flip horizontal 水平翻转
  ③. cross dissolve 淡入淡出
  ④. partial curl 翻书切换

2.scene之间数据获取
   在原视图控制器中实现方法 

-(void) prepareForSegue: (UIStoryboardSegue *)segue sender: (id)sender

在该方法内可以获取原控制器里面的属性,还可以获取目标控制器里面的属性,如: 

ViewController *startView = (ViewController*)segue.sourceViewController;
DestinationViewController *desView = (DestinationViewController*)segue.destinationViewController;

简单方式可以如下:
ViewController *startView = (ViewController*)self.presentingViewController;
DestinationViewController *desView = (DestinationViewController*)self.presentedViewController;

可以实现数据之间的传递

IOS基础知识记录四(Master-Detail Application)

回帖奖励 1.在创建主-从视图的应用程序时,Master-Detail Application无疑是一个良好的想法,
它结和表视图和详细视图,而且还能调整内容的大小。 主要还能在ipad和iphone上面跑起来
不过在ipad上面用到是弹出层(竖屏)

2.在使用xcode 4.5,ios6时,一些基本重要的方法都自动生成,只需要你填入适当的数据即可

  1. - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView

  2. (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section

  3. - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//在该方法中部分功能已实现
  4.       UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];

  5.       cell.textLabel.text=@"title"// 你需要处理标题 还有付标题 以及image
  6.       return cell;
  7. }

  8. //切换时调用方法也生成了,在该方法中可以设置在详细视图中显示的值
  9. - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender

3.在DetailViewController中实现代码
  1. - (void)configureView
  2. {
  3.     // Update the user interface for the detail item.

  4.     if (self.detailItem) {
  5.         //self.detailDescriptionLabel.text = [self.detailItem description];
  6.         //你预在详细页面显示的内容
  7.     }
  8. }

4. 在MasterViewController里面 还增加两个默认的编辑按钮和删除按钮。
  1. - (void)viewDidLoad
  2. {
  3.     [super viewDidLoad];
  4.         // Do any additional setup after loading the view, typically from a nib.
  5.     self.navigationItem.leftBarButtonItem = self.editButtonItem;

  6.     UIBarButtonItem *addButton = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self action:@selector(insertNewObject:)];
  7.    self.navigationItem.rightBarButtonItem = addButton;
  8. }


IOS基础知识记录五(简单手电筒)

一个简单的手电筒实例

1.在xcode中使用模板Single view Application 新建一个Light项目

2.在MainStoryboard.storyboard里面规划三个控件, 一个开关(UISwitch), 一个控制控制亮度(UISlider),一个控制显示(UIView. 先设置Scene中的视图的alpha = 1.0 即默认为黑色

3.分别对switchOn和slider的value changed触发同一个事件 setLightAlphaValue

  1. - (IBAction)setLightAlphaValue:(id)sender {
  2.     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  3.    
  4.     [userDefaults setBool: switchOn.on forKey: @"isOn"];
  5.     [userDefaults setFloat: slider.value forKey: @"sliderValue"];
  6.    
  7.     [userDefaults synchronize];
  8.    //以上操作只是设置客户的一些默认操作。如:设置开关的默认状态, 手电筒的亮度
  9.    //方便下次打开时, 记住上次操作的结果
  10.    
  11.     if (switchOn.isOn) {
  12.         light.alpha = slider.value;
  13.     }else {
  14.         light.alpha = 0.0;
  15.     }
  16. }

4.在viewDidLoad中调用一个初始化方法。该方法用来初始化手电筒的一些显示参数
  1. //加载上次操作结果
  2. - (void)initBirghtnessAndSwithOn {
  3.     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
  4.    
  5.     switchOn.on = [userDefaults boolForKey: @"isOn"];
  6.     slider.value = [userDefaults floatForKey: @"sliderValue"]];
  7.    
  8.     if ([userDefaults boolForKey: @"isOn"]) {
  9.         light.alpha = [userDefaults floatForKey: @"sliderValue"]];
  10.     }else {
  11.         light.alpha = 0.0;
  12.     }
  13. }

5.如果你喜欢可以把项目中一些常用的常量采用宏定义
  1. #define switchOnOff @"isOn"
  2. #define sliderValue @"sliderValue"

  3. //这样可以替换项目中的常量, 变于统一操作

IOS基础知识记录六(读写文件)

 ios对文件的读写

  1. NSString *filePath = @"/User/test.txt"
  2. //判断文件是否存在, 否则创建该文件
  3. if (![[NSFileManager alloc] fileExistsAtPath: filePath]) {
  4.         //contents 可以初始化文件时, 加入默认内容
  5.         [[NSFileManager alloc] createFileAtPath: filePath
  6.                                        contents: nil attributes: nil];
  7.     }

  8. //更新文件
  9. NSFileHandle *handler = [NSFileHandle fileHandleForUpdatingAtPath: filePath];
  10.     [handler seekToEndOfFile];//跳到文件末尾
  11.    
  12.     NSString *content = @"this is a file."
  13.     [handler writeData: [content dataUsingEncoding: NSUTF8StringEncoding]];
  14.     [handler closeFile];

  1. //读取文件
  2.   if ([[NSFileManager alloc] fileExistsAtPath: filePath]) {
  3.         NSFileHandle *handler = [NSFileHandle fileHandleForReadingAtPath: filePath];
  4.       
  5.         //content读取的新内容
  6.         NSString *content = [[NSString alloc] initWithData: [handler availableData] encoding: NSUTF8StringEncoding];
  7.         
  8.         [handler closeFile];
  9.     }

[handler closeFile];//文件读写后,关闭文件

IOS基础知识记录七(iphone手机横屏、竖屏)

iphone手机旋转屏幕时,对视图进行切换
如:iPhone手机 Music在纵向模式和横向模式显显示播放列表方式的差异
1.创建Simple View Application一个项目
然后在添加一个UIView 标签设置为 landscape view.而默认的view设置为portrait view
把新加的view 托放到和View Controller同一级

2.然后编辑portrait View 在里面设置布局和样式,设置后把portrait View 托到View Controller 同一级, 把landscape View拖放到View Controller下。开始编辑landscape View

3.编辑成功后, 可以设置该两个view里面公共的属性和方法

4.在ViewController里面设置两个视图进行切换。如:

  1. //首先在ViewController.m里面加入常量定义
  2. #define CRotate  (3.1415926/180.0)

  3. //改方法在切换屏幕时, 进行调用
  4. -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
  5.    
  6.     [super willRotateToInterfaceOrientation:toInterfaceOrientation
  7.                                    duration:duration];
  8.    
  9.     if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
  10.         self.view = landscapeView;
  11.         self.view.transform = CGAffineTransformMakeRotation(CRotate*90);
  12.         self.view.bounds = CGRectMake(0, 0, 480, 300);
  13.         
  14.     }else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
  15.         self.view = landscapeView;
  16.         self.view.transform = CGAffineTransformMakeRotation(CRotate*(-90));
  17.         self.view.bounds = CGRectMake(0, 0, 480, 300);
  18.         
  19.     }else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
  20.         self.view = portraitView;
  21.         self.view.transform = CGAffineTransformMakeRotation(0);
  22.         self.view.bounds = CGRectMake(0, 0, 320, 460);
  23.   
  24.     }
  25. }

IOS基础知识记录八(手机相机或者图片库)

 调用手机相机或者手机图像库


1.调用手机相机或者图片库要遵循协议

  1.   UIImagePickerControllerDelegate
  2.   UINavigationControllerDelegate//方便隐藏状态栏

复制代码
2.通过模态显示相机或者图片库
  1. UIImagePickerController *imagePicker;
  2.     imagePicker = [[UIImagePickerController alloc] init];
  3.    
  4.     if ([camera isOn]) {
  5.         //前置还是后置摄像头
  6.         imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
  7.         imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  8.     }else {
  9.         imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
  10.     }

  11.     imagePicker.delegate = self;
  12.   
  13.     [self presentViewController: imagePicker
  14.                        animated:YES
  15.                      completion: nil];

复制代码
注: 一般相机或者图片库时, 会全屏显示的,最好把状态栏隐藏掉
  1. [[UIApplication sharedApplication] setStatusBarHidden: YES];

复制代码
说明:
  1. [self presentViewController: imagePicker
  2.                        animated:YES
  3.                      completion: nil];
  4. //该方法是ios6新加的 替代下面方法显示模态
  5. [self presentModalViewController:<#(UIViewController *)#> animated:<#(BOOL)#>]

复制代码
3.遵循协议实现两个方法
  1. //UIImagePickerControllerDelegate
  2. -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
  3.    
  4.     [[UIApplication sharedApplication] setStatusBarHidden: NO];
  5.     [self dismissViewControllerAnimated: YES
  6.                              completion: nil];
  7.     //here you code
  8. }

  9. //UIImagePickerControllerDelegate
  10. -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
  11.     [[UIApplication sharedApplication] setStatusBarHidden:NO];
  12.    
  13.     [self dismissViewControllerAnimated: YES completion: nil];
  14. }


IOS基础知识记录九(调用AddressBook地址博信息)

 1.首先加入包AddressBookUI、AddressBook并引入类。其次加入地址薄调用遵循的协议

  1. #import <AddressBookUI/AddressBookUI.h>
  2. #import <AddressBook/AddressBook.h>

  3. //协议
  4. ABPeoplePickerNavigationControllerDelegate

复制代码
2.实现协议的部分方法,用来处理选择地址薄信息或者关闭地址操作等
  1. //当地址关闭时处理部分信息: 如果关闭模态等,在第七记录中已经写过
  2. -(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker

  3. //处理选择地址薄后怎么解析person信息
  4. -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person

复制代码
3.解析选择的地址薄信息
  1. -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController *)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
  2.     NSString *firstName, *lastName;
  3.    
  4. //对于地址薄中的firstName, lastName都是唯一的不会重复,故直接转化字符串
  5.     firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
  6.     lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
  7.    
  8.       //电话是多个, 要用数组来处理
  9.     ABMultiValueRef telRef;
  10.         telRef = ABRecordCopyValue(person,  kABPersonPhoneProperty);
  11.     if (ABMultiValueGetCount(telRef) > 0) {
  12.         NSString * tel = (__bridge NSString *)ABMultiValueCopyValueAtIndex(telRef, 1);
  13.     }
  14.     //Email 和电话类似
  15.     ABMultiValueRef emailRef;
  16.     emailRef = ABRecordCopyValue(person, kABPersonEmailProperty);
  17.     if (ABMultiValueGetCount(emailRef) > 0) {
  18.         email.text = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emailRef, 0);
  19.     }
  20.    
  21. //而相对于地址来说比较复杂。 地址信息包含信息量比较大
  22.     ABMultiValueRef addressRef;
  23.     NSDictionary *addressDic;
  24.     NSString *zipCode;
  25.    
  26.     addressRef = ABRecordCopyValue(person, kABPersonAddressProperty);
  27.     if (ABMultiValueGetCount(addressRef) > 0) {
  28.         addressDic = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(addressRef, 0);
  29.         zipCode = [addressDic objectForKey: @"ZIP"];
  30.         
  31.     }

  32.     //关闭模态
  33.     [self dismissViewControllerAnimated: YES completion: nil];
  34.     return  NO;
  35. }

IOS基础知识记录十(调用Google Map)

IOS简单调用Google地图

1.首先加入地图依赖的两个包

  1. #import <MapKit/MapKit.h>
  2. #import <CoreLocation/CoreLocation.h>

复制代码
2.确定位置还必须依赖你的zipCode和地址Dictionary,zipCode可以根据Google提供的方法获取你所在位置的经度和维度, 地址Dictionary可以在Map上面精确的标注出来你的位置,
实现发现如下:
  1. - (void)showMap:(NSString *)zipCode withAddress:(NSDictionary *)address {
  2.     NSString *url, *result;
  3.     NSArray *dataArray;
  4.     double latitude;
  5.     double longitude;
  6.    
  7.     MKCoordinateRegion mapRegion;
  8.     //根据Google提供zip url
  9.     url = [[NSString alloc] initWithFormat: @"http://maps.google.com/maps/geo?output=csv&q=%@", zipCode];
  10.    
  11.     //根据url获取Google提供的方法一个逗号分割的经度、维度字符串
  12.     result = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:url]
  13.                                                   encoding:NSUTF8StringEncoding
  14.                                                      error:nil];
  15.     //把逗号分隔字符串转化为数组(类似java里面的split)
  16.     dataArray = [url componentsSeparatedByString: @","];
  17.    
  18.     //Google返回的数据是, 如: ’2, 3, 4, 5‘后面两位数字分别是latitude、longitude
  19.     if ([dataArray count] == 4) {
  20.         latitude = [[dataArray objectAtIndex: 2] doubleValue];
  21.         longitude = [[dataArray objectAtIndex:3] doubleValue];
  22.         
  23.         mapRegion.center.latitude = latitude;
  24.         mapRegion.center.longitude = longitude;
  25.         mapRegion.span.latitudeDelta = 0.2;
  26.         mapRegion.span.longitudeDelta = 0.2;
  27.         
  28.         //设置你的维度和经度后 重新绘制地图
  29.         [map setRegion: mapRegion animated:YES];
  30.         
  31.         //以下是地图中标示你的地址
  32.         if (zipAnnotation != nil) {
  33.             [map removeAnnotation: zipAnnotation];
  34.         }
  35.         
  36.         zipAnnotation = [[MKPlacemark alloc] initWithCoordinate:mapRegion.center
  37.                                               addressDictionary:fullAddress];
  38.         [map addAnnotation: zipAnnotation];
  39.     }
  40. }

3.以上设置后, 便可在你的iphone模拟器上面显示地图以及你所在的位置.

原文地址:https://www.cnblogs.com/ioschen/p/3316519.html