1:运行程序报the file couldn't be opened because you don't have permission to view it

  1. 解决办法:项目—>targets->build settings->build options->changed the value of the "Compiler for C/C++/Objective-C" to Default Compiler.

2:百度地图引用

  1. .1如图引用的是.framework形式开发包时,引入的命名空间则是
  2. #import <BaiduMapAPI/BMapKit.h>//引入所有的头文件
  3. #import <BaiduMapAPI/BMKMapView.h>//只引入所需的单个头文件
  4. 如果是引入用的是.a形式开发包时,引入的命名空间则是
  5. #import “BMapKit.h"
  6.  
  7. .2百度地图现在提供的两个.framework的包,一个是真机一个是测试机,可以使用终端的命令把它合成一个;

3:自定义大头针百度地图

  1. - (void)viewDidLoad {
  2. [super viewDidLoad];
  3.  
  4. //百度地图初始化
  5. _mapView=[[BMKMapView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT-NAVBARHEIGHT)];
  6. _mapView.delegate=self;
  7. [self.view addSubview:_mapView];
  8.  
  9. //标出坐标点
  10. [self addPointAnnotation];
  11. }
  12.  
  13. //添加标注
  14. - (void)addPointAnnotation
  15. {
  16. for (int i=; i<self.coordinates.count; i++) {
  17. coordinateBean *model=self.coordinates[i];
  18.  
  19. BMKPointAnnotation* pointAnnotation = [[BMKPointAnnotation alloc]init];
  20. CLLocationCoordinate2D coor;
  21. coor.latitude = model.latitude;
  22. coor.longitude = model.longitude;
  23. pointAnnotation.coordinate = coor;
  24.  
  25. //通过title来起到传值的作用
  26. pointAnnotation.title=[NSString stringWithFormat:@"%d",i];
  27. [_mapView addAnnotation:pointAnnotation];
  28. //显示弹出窗
  29. [_mapView selectAnnotation:pointAnnotation animated:YES];
  30.  
  31. //判断那个是中心,没有则0必传参数
  32. if (i==self.selectIndex) {
  33. BMKCoordinateRegion region; ////表示范围的结构体
  34. region.center.latitude = model.latitude;// 中心中
  35. region.center.longitude = model.longitude;
  36. region.span.latitudeDelta = ;//经度范围(设置为0.1表示显示范围为0.2的纬度范围)
  37. region.span.longitudeDelta = ;//纬度范围
  38.  
  39. [_mapView setRegion:region];
  40. }
  41. }
  42. }
  43. //处理自定义弹出视图
  44. - (BMKAnnotationView *)mapView:(BMKMapView *)mapView viewForAnnotation:(id <BMKAnnotation>)annotation
  45. {
  46. if ([annotation isKindOfClass:[BMKPointAnnotation class]]) {
  47. BMKPinAnnotationView *newAnnotationView = [[BMKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"myrenameMark"];
  48. newAnnotationView.pinColor = BMKPinAnnotationColorPurple;
  49. newAnnotationView.animatesDrop = YES;// 设置该标注点动画显示
  50.  
  51. newAnnotationView.image = [UIImage imageNamed:self.mapPointImageName]; //把大头针换成别的图片
  52.  
  53. int selectIndex=[((BMKPointAnnotation *)annotation).title intValue];
  54. //获得值
  55. coordinateBean *model=[self.coordinates objectAtIndex:[((BMKPointAnnotation *)annotation).title intValue]];
  56.  
  57. UIView *popView=[[UIView alloc]initWithFrame:CGRectMake(, , , )];
  58.  
  59. UIImage *img=[UIImage imageNamed:@"mapViewBackground"];
  60. UIEdgeInsets edge=UIEdgeInsetsMake(, , , );
  61. img=[img resizableImageWithCapInsets:edge resizingMode:UIImageResizingModeStretch];
  62. UIImageView *myimage=[[UIImageView alloc] initWithImage:img];
  63. myimage.frame=CGRectMake(, , , );
  64. myimage.userInteractionEnabled=YES;
  65.  
  66. [popView addSubview:myimage];
  67.  
  68. //自定义显示的内容
  69. UILabel *driverName = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
  70. driverName.backgroundColor=[UIColor clearColor];
  71. driverName.text=model.title;
  72. driverName.font = [UIFont systemFontOfSize:];
  73. driverName.textColor = [UIColor blackColor];
  74. driverName.textAlignment = NSTextAlignmentLeft;
  75. [myimage addSubview:driverName];
  76.  
  77. UILabel *carName = [[UILabel alloc]initWithFrame:CGRectMake(, , , )];
  78. carName.backgroundColor=[UIColor clearColor];
  79. carName.text=model.comments;
  80. carName.font = [UIFont systemFontOfSize:];
  81. carName.textColor = [UIColor blackColor];
  82. carName.textAlignment = NSTextAlignmentLeft;
  83. [myimage addSubview:carName];
  84.  
  85. BMKActionPaopaoView *pView = [[BMKActionPaopaoView alloc]initWithCustomView:popView];
  86. pView.frame = CGRectMake(, , , );
  87. ((BMKPinAnnotationView*)newAnnotationView).paopaoView = nil;
  88. ((BMKPinAnnotationView*)newAnnotationView).paopaoView = pView;
  89. newAnnotationView.tag=selectIndex+;
  90.  
  91. return newAnnotationView;
  92. }
  93. return nil;
  94. }
  95.  
  96. - (void)didReceiveMemoryWarning {
  97. [super didReceiveMemoryWarning];
  98. // Dispose of any resources that can be recreated.
  99. }
  100. /**
  101. * @author wujunyang, 15-05-12 13:05:05
  102. *
  103. * @brief 跟对百度地图的处理
  104. * @param animated <#animated description#>
  105. */
  106. -(void)viewWillAppear:(BOOL)animated
  107. {
  108. [_mapView viewWillAppear];
  109.  
  110. _mapView.delegate=self;
  111. _locationService.delegate=self;
  112. }
  113. /**
  114. * @author wujunyang, 15-01-06 10:01:53
  115. *
  116. * 跟对百度地图的处理
  117. *
  118. * @param animated <#animated description#>
  119. */
  120. -(void)viewWillDisappear:(BOOL)animated
  121. {
  122. [_mapView viewWillDisappear];
  123. _mapView.delegate=nil;
  124. _locationService.delegate=nil
  125. }
  126.  
  127. 其中有个自定义model:
  128.  
  129. @interface coordinateBean : NSObject
  130. //纬度
  131. @property(assign,nonatomic)float latitude;
  132. //经度
  133. @property(assign,nonatomic)float longitude;
  134. //标题
  135. @property(strong,nonatomic)NSString *title;
  136. //注解
  137. @property(strong,nonatomic)NSString *comments;
  138. @end

 4:自动隐藏和显示工具栏和导航条

  1. toolbar属性、toolbarItems与上一讲的navigationBarnavigationItem类似。只不过toolbarItems没有navigationItem的左右区分,它就自己一个人在做事,相当于没有下属。可以在toolbar上设置很多,比如背景颜色、背景图片、背景样式、大小位置(不过有些貌似设置无效),当然和navigationBar一样,对于它的是否显示和隐藏是由它的老爸即navigationController控制的。
  2.  
  3. 所以[self.navigationController setNavigationBarHidden:YES animated:YES];也会把底部的toolBarItems给隐藏起来,如果要隐藏导航又不想底部toolBarItems被隐藏掉,可以用普通的view替代toolBarItems;
  4.  
  5. 首先在viewDidLoad里设置toolBarHidden = NO, 默认是YES(隐藏的)
  6.  
  7. 为了让toolbar显示,需要设置为NO(不隐藏)。
  8.  
  9. - (void)viewDidLoad
  10. {
  11. [super viewDidLoad];
  12. self.title = @"隐藏导航栏";
  13. // self.toolbarItems
  14. self.navigationController.toolbar.barStyle = self.toolBar.barStyle;
  15. self.navigationController.toolbarHidden = NO;
  16.  
  17. [self.navigationController.toolbar setTranslucent:YES];
  18.  
  19. self.toolbarItems = [[[NSMutableArray alloc] initWithArray:self.toolBar.items] autorelease];
  20. }
  21.  
  22. 在点击中间button的时候的显示和隐藏navigation bartoolBar
  23.  
  24. 实现代码如下:
  25.  
  26. - (IBAction)toggleNavigationBar:(id)sender
  27. {
  28. //Check the current state of the navigation bar...
  29. BOOL navBarState = [self.navigationController isNavigationBarHidden];
  30. //Set the navigationBarHidden to the opposite of the current state.
  31. [self.navigationController setNavigationBarHidden:!navBarState animated:YES];
  32. [self.navigationController setToolbarHidden:!navBarState animated:YES];
  33. //Change the label on the button.
  34. if (navBarState)
  35. {
  36. [button setTitle:@"隐藏 Navigationr and toolbar" forState:UIControlStateNormal];
  37. [button setTitle:@"隐藏 Navigation Bar toolbar" forState:UIControlStateHighlighted];
  38. }
  39. else
  40. {
  41. [button setTitle:@"显示 Navigation Bar toolbar" forState:UIControlStateNormal];
  42. [button setTitle:@"显示 Navigation Bar toolbar" forState:UIControlStateHighlighted];
  43. }
  44. }

5:View代码结构的一些建议

  1. viewDidload里面只做addSubview的事情,然后在viewWillAppear里面做布局的事情,最后在viewDidAppear里面做Notification的监听之类的事情。至于属性的初始化,则交给getter去做。
  2.  
  3. @interface CustomObject()
  4. @property (nonatomic, strong) UILabel *label;
  5. @end
  6.  
  7. @implement
  8.  
  9. #pragma mark - life cycle
  10.  
  11. - (void)viewDidLoad
  12. {
  13. [super viewDidLoad];
  14.  
  15. [self.view addSubview:self.label];
  16. }
  17.  
  18. - (void)viewWillAppear:(BOOL)animated
  19. {
  20. [super viewWillAppear:animated];
  21.  
  22. self.label.frame = CGRectMake(, , , );
  23. }
  24.  
  25. #pragma mark - getters and setters
  26.  
  27. - (UILabel *)label
  28. {
  29. if (_label == nil) {
  30. _label = [[UILabel alloc] init];
  31. _label.text = @"";
  32. _label.font = [UIFont systemFontOfSize:];
  33. ... ...
  34. }
  35. return label;
  36. }
  37. @end
  38.  
  39. 注意:*重点,在get方法里面不能写self.noLabel;千万不要用“点”语法,这样会造成get方法死循环,因为“点”语法就是调用的get方法,所以要用下划线属性名的方法得到对象(在内存这其实是一个指针)。
  1. @interface MasonryViewController ()
  2. @property(nonatomic,strong)UIView *conView;
  3. @property(nonatomic,assign)int intstate;
  4. @end
  5.  
  6. @implementation MasonryViewController
  7.  
  8. - (void)viewDidLoad {
  9. [super viewDidLoad];
  10.  
  11. [self.view addSubview:self.conView];
  12. }
  13. //懒加载
  14. -(UIView *)conView
  15. {
  16. if(_conView==nil)
  17. {
  18. _conView=[[UIView alloc]init];
  19. _conView.backgroundColor=[UIColor redColor];
  20. }
  21. return _conView;
  22. }
  23.  
  24. -(int)intstate
  25. {
  26. _intstate=;
  27. return _intstate;
  28. }
  29.  
  30. //布局约束
  31. -(void)viewDidLayoutSubviews
  32. {
  33. [self.conView mas_makeConstraints:^(MASConstraintMaker *make) {
  34. make.top.equalTo(self.view.mas_top).with.offset();
  35. make.left.equalTo(self.view.mas_left).with.offset();
  36. make.right.equalTo(self.view.mas_right).with.offset();
  37. make.height.equalTo(@);
  38. }];
  39. }
  40.  
  41. - (void)didReceiveMemoryWarning {
  42. [super didReceiveMemoryWarning];
  43. }

6: iOS中的生成随机数方法

  1. 生成0-x之间的随机正整数
  2.  
  3. int value =arc4random_uniform(x );
  4.  
  5. 生成随机正整数
  6.  
  7. int value = arc4random()
  8.  
  9. 通过arc4random() 获取0x-1之间的整数的代码如下:
  10.  
  11. int value = arc4random() % x;
  12.  
  13. 获取1x之间的整数的代码如下:
  14.  
  15. int value = (arc4random() % x) + ;
  16.  
  17. 最后如果想生成一个浮点数,可以在项目中定义如下宏:
  18.  
  19. #define ARC4RANDOM_MAX 0x100000000
  20.  
  21. 然后就可以使用arc4random() 来获取0100之间浮点数了(精度是rand()的两倍),代码如下:
  22.  
  23. double val = floorf(((double)arc4random() / ARC4RANDOM_MAX) * 100.0f);
  24.  
  25. 实例(从数组中随机显示出一个背景图,再通过网络加载显示出来):
  26.  
  27. self.bgView=[[UIImageView alloc] initWithFrame:CGRectMake(, , SCREEN_WIDTH, SCREEN_HEIGHT)];
  28. self.bgView.image=[UIImage imageNamed:@"AppBg"];
  29. [self.view addSubview:self.bgView];
  30. [self.view sendSubviewToBack:self.bgView];
  31.  
  32. NSDictionary *params=[[NSDictionary alloc] init];
  33. [[HomeMainNetAPIManager sharedManager] getBackgroundImage:params andBlock:^(id data, NSError *error) {
  34. if (!error&&data) {
  35. BackgroundImageBean *groundImagebean =(BackgroundImageBean *)data;
  36. int dataNum=groundImagebean.data.count;
  37. if (groundImagebean.data&&dataNum>) {
  38. int r=arc4random_uniform(dataNum);
  39. GroundImageBean *curBean=groundImagebean.data[r];
  40. [self.bgView sd_setImageWithURL:[NSURL URLWithString:curBean.ImgUrl] placeholderImage:[UIImage imageNamed:@"AppBg"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
  41. dispatch_async(dispatch_get_main_queue(), ^{
  42. self.bgView.image=image;
  43. });
  44. }];
  45. }
  46.  
  47. }
  48. }];

7:沙盒路径知识整理

  1. 模拟器的路径从之前的~/Library/Application Support/iPhone Simulator移动到了~/Library/Developer/CoreSimulator/Devices/
  2.  
  3. 文件都在个人用户名文件夹下的一个隐藏文件夹里,中文叫资源库,他的目录其实是Library
  4.  
  5. 因为应用是在沙箱(sandbox)中的,在文件读写权限上受到限制,只能在几个目录下读写文件:
  6. Documents:应用中用户数据可以放在这里,iTunes备份和恢复的时候会包括此目录
  7. tmp:存放临时文件,iTunes不会备份和恢复此目录,此目录下文件可能会在应用退出后删除
  8. Library/Caches:存放缓存文件,iTunes不会备份此目录,此目录下文件不会在应用退出删除
  9.  
  10. iTunes在与iPhone同步时,备份所有的DocumentsLibrary文件。
  11. iPhone在重启时,会丢弃所有的tmp文件。
  12.  
  13. 查看方法:
  14. 方法1、可以设置显示隐藏文件,然后在Finder下直接打开。设置查看隐藏文件的方法如下:打开终端,输入命名
  15. ()显示Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool true
  16. ()隐藏Mac隐藏文件的命令:defaults write com.apple.finder AppleShowAllFiles -bool false
  17. ()输完单击Enter键,退出终端,重新启动Finder就可以了 重启Finder:鼠标单击窗口左上角的苹果标志-->强制退出-->Finder-->
  18. 现在能看到资源库文件夹了。
  19. 打开资源库后找到/Application Support/iPhone Simulator/文件夹。这里面就是模拟器的各个程序的沙盒目录了。
  20. 方法2、这种方法更方便,在Finder上点->前往->前往文件夹,输入/Users/username/Library/Application Support/iPhone Simulator/ 前往。
  21. username这里写用户名。
  22.  
  23. 自定义类返回各目录路径:
  24.  
  25. #import <Foundation/Foundation.h>
  26.  
  27. @interface ICSandboxHelper : NSObject
  28.  
  29. + (NSString *)homePath; // 程序主目录,可见子目录(3个):Documents、Library、tmp
  30. + (NSString *)appPath; // 程序目录,不能存任何东西
  31. + (NSString *)docPath; // 文档目录,需要ITUNES同步备份的数据存这里,可存放用户数据
  32. + (NSString *)libPrefPath; // 配置目录,配置文件存这里
  33. + (NSString *)libCachePath; // 缓存目录,系统永远不会删除这里的文件,ITUNES会删除
  34. + (NSString *)tmpPath; // 临时缓存目录,APP退出后,系统可能会删除这里的内容
  35. + (BOOL)hasLive:(NSString *)path; //判断目录是否存在,不存在则创建
  36.  
  37. 实现代码:
  38.  
  39. #import "ICSandboxHelper.h"
  40.  
  41. @implementation ICSandboxHelper
  42.  
  43. + (NSString *)homePath{
  44. return NSHomeDirectory();
  45. }
  46.  
  47. + (NSString *)appPath
  48. {
  49. NSArray * paths = NSSearchPathForDirectoriesInDomains(NSApplicationDirectory, NSUserDomainMask, YES);
  50. return [paths objectAtIndex:];
  51. }
  52.  
  53. + (NSString *)docPath
  54. {
  55. NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  56. return [paths objectAtIndex:];
  57. }
  58.  
  59. + (NSString *)libPrefPath
  60. {
  61. NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  62. return [[paths objectAtIndex:] stringByAppendingFormat:@"/Preference"];
  63. }
  64.  
  65. + (NSString *)libCachePath
  66. {
  67. NSArray * paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  68. return [[paths objectAtIndex:] stringByAppendingFormat:@"/Caches"];
  69. }
  70.  
  71. + (NSString *)tmpPath
  72. {return [NSHomeDirectory() stringByAppendingFormat:@"/tmp"];
  73. }
  74.  
  75. + (BOOL)hasLive:(NSString *)path
  76. {
  77. if ( NO == [[NSFileManager defaultManager] fileExistsAtPath:path] )
  78. {
  79. return [[NSFileManager defaultManager] createDirectoryAtPath:path
  80. withIntermediateDirectories:YES
  81. attributes:nil
  82. error:NULL];
  83. }
  84.  
  85. return NO;
  86. }

IOS开发基础知识--碎片13的更多相关文章

  1. IOS开发基础知识碎片-导航

    1:IOS开发基础知识--碎片1 a:NSString与NSInteger的互换 b:Objective-c中集合里面不能存放基础类型,比如int string float等,只能把它们转化成对象才可 ...

  2. IOS开发基础知识--碎片14

    1:ZIP文件压缩跟解压,使用ZipArchive 创建/添加一个zip包 ZipArchive* zipFile = [[ZipArchive alloc] init]; //次数得zipfilen ...

  3. IOS开发基础知识--碎片33

    1:AFNetworking状态栏网络请求效果 直接在AppDelegate里面didFinishLaunchingWithOptions进行设置 [[AFNetworkActivityIndicat ...

  4. IOS开发基础知识--碎片42

    1:报thread 1:exc_bad_access(code=1,address=0x70********) 闪退 这种错误通常是内存管理的问题,一般是访问了已经释放的对象导致的,可以开启僵尸对象( ...

  5. IOS开发基础知识--碎片50

      1:Masonry 2个或2个以上的控件等间隔排序 /** * 多个控件固定间隔的等间隔排列,变化的是控件的长度或者宽度值 * * @param axisType 轴线方向 * @param fi ...

  6. IOS开发基础知识--碎片3

    十二:判断设备 //设备名称 return [UIDevice currentDevice].name; //设备型号,只可得到是何设备,无法得到是第几代设备 return [UIDevice cur ...

  7. IOS开发基础知识--碎片11

    1:AFNetwork判断网络状态 #import “AFNetworkActivityIndicatorManager.h" - (BOOL)application:(UIApplicat ...

  8. IOS开发基础知识--碎片16

    1:Objective-C语法之动态类型(isKindOfClass, isMemberOfClass,id) 对象在运行时获取其类型的能力称为内省.内省可以有多种方法实现. 判断对象类型 -(BOO ...

  9. IOS开发基础知识--碎片19

    1:键盘事件顺序 UIKeyboardWillShowNotification // 键盘显示之前 UIKeyboardDidShowNotification // 键盘显示完成后 UIKeyboar ...

随机推荐

  1. ASP.NET Core的配置(4):多样性的配置来源[上篇]

    较之传统通过App.config和Web.config这两个XML文件承载的配置系统,ASP.NET Core采用的这个全新的配置模型的最大一个优势就是针对多种不同配置源的支持.我们可以将内存变量.命 ...

  2. Web APi之EntityFramework【CRUD】(三)

    前言 之前我们系统学习了EntityFramework,个人觉得有些东西不能学了就算完了,必须要学以致用,在Web API上也少不了增(C).删(D).改(U).查(R).鉴于此,我们通过EF来实现W ...

  3. 引用类型-RegExp类型

    JavaScript高级程序设计(第三版)笔记-第五章-RegExp类型 定义正则表达式有两种方式 1.用字面量形式定义正则表达式: ver expression = /patterns/flags; ...

  4. 【转】java中注解的使用与实例

    原文:http://www.cnblogs.com/pepcod/archive/2013/02/20/2918719.html java 注解,从名字上看是注释,解释.但功能却不仅仅是注释那么简单. ...

  5. Redis碎碎念

    1. 关于Cluster cluster_known_nodes:4 cluster_size:3 说明集群中总共有4个节点:集群的size是3,相当于3个主节点参与了槽位分配 2. 如何查看key的 ...

  6. C算法编程题(二)正螺旋

    前言 上一篇<C算法编程题(一)扑克牌发牌> 写东西前总是喜欢吐槽一些东西,还是多啰嗦几句吧,早上看了一篇博文<谈谈外企涨工资那些事>,里面楼主讲到外企公司包含的五类人,其实不 ...

  7. Lua 学习笔记(八)错误(error)

    Lua所遇到的任何未预期条件都会引发一个错误.因此在发生错误时不能简单的崩溃或着退出,而是结束当前程序块并返回应用程序.当错误引发时进行恰当的处理是最合适的,然而这个阶段伴随着错误的捕获.错误的处理. ...

  8. 让自己也能使用Canvas

    <canvas> 是 HTML5 新增的元素,可使用JavaScript脚本来绘制图形.例如:画图,合成照片,创建动画甚至实时视频处理与渲染. 兼容性方面,除了一些骨灰级浏览器IE6.IE ...

  9. 《图解Spark:核心技术与案例实战》介绍及书附资源

    本书中所使用到的测试数据.代码和安装包放在百度盘提供 下载 ,地址为https://pan.baidu.com/s/1o8ydtKA 密码:imaa 另外在百度盘提供本书附录  下载 ,地址为http ...

  10. 相克军_Oracle体系_随堂笔记013-字符集

    linux环境下: [root@single ~]# locale LANG=en_US.UTF-8 LC_CTYPE="en_US.UTF-8" …… windows环境下: C ...