(转载)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时,一些基本重要的方法都自动生成,只需要你填入适当的数据即可
- - (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
- - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{//在该方法中部分功能已实现
- UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
- cell.textLabel.text=@"title"// 你需要处理标题 还有付标题 以及image
- return cell;
- }
- //切换时调用方法也生成了,在该方法中可以设置在详细视图中显示的值
- - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
- - (void)configureView
- {
- // Update the user interface for the detail item.
- if (self.detailItem) {
- //self.detailDescriptionLabel.text = [self.detailItem description];
- //你预在详细页面显示的内容
- }
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- self.navigationItem.leftBarButtonItem = self.editButtonItem;
- UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(insertNewObject:)];
- self.navigationItem.rightBarButtonItem = addButton;
- }
IOS基础知识记录五(简单手电筒)
一个简单的手电筒实例
1.在xcode中使用模板Single view Application 新建一个Light项目
2.在MainStoryboard.storyboard里面规划三个控件, 一个开关(UISwitch), 一个控制控制亮度(UISlider),一个控制显示(UIView. 先设置Scene中的视图的alpha = 1.0 即默认为黑色
3.分别对switchOn和slider的value changed触发同一个事件 setLightAlphaValue
- - (IBAction)setLightAlphaValue:(id)sender {
- NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
-
- [userDefaults setBool: switchOn.on forKey: @"isOn"];
- [userDefaults setFloat: slider.value forKey: @"sliderValue"];
-
- [userDefaults synchronize];
- //以上操作只是设置客户的一些默认操作。如:设置开关的默认状态, 手电筒的亮度
- //方便下次打开时, 记住上次操作的结果
-
- if (switchOn.isOn) {
- light.alpha = slider.value;
- }else {
- light.alpha = 0.0;
- }
- }
- //加载上次操作结果
- - (void)initBirghtnessAndSwithOn {
- NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
-
- switchOn.on = [userDefaults boolForKey: @"isOn"];
- slider.value = [userDefaults floatForKey: @"sliderValue"]];
-
- if ([userDefaults boolForKey: @"isOn"]) {
- light.alpha = [userDefaults floatForKey: @"sliderValue"]];
- }else {
- light.alpha = 0.0;
- }
- }
- #define switchOnOff @"isOn"
- #define sliderValue @"sliderValue"
- //这样可以替换项目中的常量, 变于统一操作
IOS基础知识记录六(读写文件)
ios对文件的读写
- NSString *filePath = @"/User/test.txt"
- //判断文件是否存在, 否则创建该文件
- if (![[NSFileManager alloc] fileExistsAtPath: filePath]) {
- //contents 可以初始化文件时, 加入默认内容
- [[NSFileManager alloc] createFileAtPath: filePath
- contents: nil attributes: nil];
- }
- //更新文件
- NSFileHandle *handler = [NSFileHandle fileHandleForUpdatingAtPath: filePath];
- [handler seekToEndOfFile];//跳到文件末尾
-
- NSString *content = @"this is a file."
- [handler writeData: [content dataUsingEncoding: NSUTF8StringEncoding]];
- [handler closeFile];
- //读取文件
- if ([[NSFileManager alloc] fileExistsAtPath: filePath]) {
- NSFileHandle *handler = [NSFileHandle fileHandleForReadingAtPath: filePath];
-
- //content读取的新内容
- NSString *content = [[NSString alloc] initWithData: [handler availableData] encoding: NSUTF8StringEncoding];
-
- [handler closeFile];
- }
[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里面设置两个视图进行切换。如:
- //首先在ViewController.m里面加入常量定义
- #define CRotate (3.1415926/180.0)
- //改方法在切换屏幕时, 进行调用
- -(void) willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration {
-
- [super willRotateToInterfaceOrientation:toInterfaceOrientation
- duration:duration];
-
- if (toInterfaceOrientation == UIInterfaceOrientationLandscapeRight) {
- self.view = landscapeView;
- self.view.transform = CGAffineTransformMakeRotation(CRotate*90);
- self.view.bounds = CGRectMake(0, 0, 480, 300);
-
- }else if (toInterfaceOrientation == UIInterfaceOrientationLandscapeLeft) {
- self.view = landscapeView;
- self.view.transform = CGAffineTransformMakeRotation(CRotate*(-90));
- self.view.bounds = CGRectMake(0, 0, 480, 300);
-
- }else if (toInterfaceOrientation == UIInterfaceOrientationPortrait) {
- self.view = portraitView;
- self.view.transform = CGAffineTransformMakeRotation(0);
- self.view.bounds = CGRectMake(0, 0, 320, 460);
-
- }
- }
IOS基础知识记录八(手机相机或者图片库)
调用手机相机或者手机图像库
1.调用手机相机或者图片库要遵循协议
- UIImagePickerControllerDelegate
- UINavigationControllerDelegate//方便隐藏状态栏
- UIImagePickerController *imagePicker;
- imagePicker = [[UIImagePickerController alloc] init];
-
- if ([camera isOn]) {
- //前置还是后置摄像头
- imagePicker.cameraDevice = UIImagePickerControllerCameraDeviceFront;
- imagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
- }else {
- imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
- }
- imagePicker.delegate = self;
-
- [self presentViewController: imagePicker
- animated:YES
- completion: nil];
- [[UIApplication sharedApplication] setStatusBarHidden: YES];
- [self presentViewController: imagePicker
- animated:YES
- completion: nil];
- //该方法是ios6新加的 替代下面方法显示模态
- [self presentModalViewController:<#(UIViewController *)#> animated:<#(BOOL)#>]
- //UIImagePickerControllerDelegate
- -(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info {
-
- [[UIApplication sharedApplication] setStatusBarHidden: NO];
- [self dismissViewControllerAnimated: YES
- completion: nil];
- //here you code
- }
- //UIImagePickerControllerDelegate
- -(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker {
- [[UIApplication sharedApplication] setStatusBarHidden:NO];
-
- [self dismissViewControllerAnimated: YES completion: nil];
- }
IOS基础知识记录九(调用AddressBook地址博信息)
1.首先加入包AddressBookUI、AddressBook并引入类。其次加入地址薄调用遵循的协议
- #import <AddressBookUI/AddressBookUI.h>
- #import <AddressBook/AddressBook.h>
- //协议
- ABPeoplePickerNavigationControllerDelegate
- //当地址关闭时处理部分信息: 如果关闭模态等,在第七记录中已经写过
- -(void)peoplePickerNavigationControllerDidCancel:(ABPeoplePickerNavigationController *)peoplePicker
- //处理选择地址薄后怎么解析person信息
- -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController
*)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person
- -(BOOL)peoplePickerNavigationController:(ABPeoplePickerNavigationController
*)peoplePicker shouldContinueAfterSelectingPerson:(ABRecordRef)person {
- NSString *firstName, *lastName;
-
- //对于地址薄中的firstName, lastName都是唯一的不会重复,故直接转化字符串
- firstName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonFirstNameProperty);
- lastName = (__bridge NSString *)ABRecordCopyValue(person, kABPersonLastNameProperty);
-
- //电话是多个, 要用数组来处理
- ABMultiValueRef telRef;
- telRef = ABRecordCopyValue(person, kABPersonPhoneProperty);
- if (ABMultiValueGetCount(telRef) > 0) {
- NSString * tel = (__bridge NSString *)ABMultiValueCopyValueAtIndex(telRef, 1);
- }
- //Email 和电话类似
- ABMultiValueRef emailRef;
- emailRef = ABRecordCopyValue(person, kABPersonEmailProperty);
- if (ABMultiValueGetCount(emailRef) > 0) {
- email.text = (__bridge NSString *)ABMultiValueCopyValueAtIndex(emailRef, 0);
- }
-
- //而相对于地址来说比较复杂。 地址信息包含信息量比较大
- ABMultiValueRef addressRef;
- NSDictionary *addressDic;
- NSString *zipCode;
-
- addressRef = ABRecordCopyValue(person, kABPersonAddressProperty);
- if (ABMultiValueGetCount(addressRef) > 0) {
- addressDic = (__bridge NSDictionary *)ABMultiValueCopyValueAtIndex(addressRef, 0);
- zipCode = [addressDic objectForKey: @"ZIP"];
-
- }
- //关闭模态
- [self dismissViewControllerAnimated: YES completion: nil];
- return NO;
- }
IOS基础知识记录十(调用Google Map)
IOS简单调用Google地图
1.首先加入地图依赖的两个包
- #import <MapKit/MapKit.h>
- #import <CoreLocation/CoreLocation.h>
实现发现如下:
- - (void)showMap:(NSString *)zipCode withAddress:(NSDictionary *)address {
- NSString *url, *result;
- NSArray *dataArray;
- double latitude;
- double longitude;
-
- MKCoordinateRegion mapRegion;
- //根据Google提供zip url
- url = [[NSString alloc] initWithFormat: @"http://maps.google.com/maps/geo?output=csv&q=%@", zipCode];
-
- //根据url获取Google提供的方法一个逗号分割的经度、维度字符串
- result = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:url]
- encoding:NSUTF8StringEncoding
- error:nil];
- //把逗号分隔字符串转化为数组(类似java里面的split)
- dataArray = [url componentsSeparatedByString: @","];
-
- //Google返回的数据是, 如: ’2, 3, 4, 5‘后面两位数字分别是latitude、longitude
- if ([dataArray count] == 4) {
- latitude = [[dataArray objectAtIndex: 2] doubleValue];
- longitude = [[dataArray objectAtIndex:3] doubleValue];
-
- mapRegion.center.latitude = latitude;
- mapRegion.center.longitude = longitude;
- mapRegion.span.latitudeDelta = 0.2;
- mapRegion.span.longitudeDelta = 0.2;
-
- //设置你的维度和经度后 重新绘制地图
- [map setRegion: mapRegion animated:YES];
-
- //以下是地图中标示你的地址
- if (zipAnnotation != nil) {
- [map removeAnnotation: zipAnnotation];
- }
-
- zipAnnotation = [[MKPlacemark alloc] initWithCoordinate:mapRegion.center
- addressDictionary:fullAddress];
- [map addAnnotation: zipAnnotation];
- }
- }