iOS App 开发问题汇总(一)

1.Storyboard中的ViewController上添加一个自定义的view,声明为IBOutlet然后用代码改变view的Frame,打印输出Frame的值确实改变了,但是模拟器上的视图的Frame还是没有改变。

解决办法:Google找到Stackoverflow上有人说是选中了Auto layout的原因,取消之后确实生效了。PS:但是不知道问题的原因是什么。

Reference:
o Setting the frame of an UIView does not work

2.在switch语句中,如果在case中要定义变量的话要加上大括号。

原因:Case statements are only ‘labels’. This means the compiler will interpret this as a jump directly to the label.The problem here is one of scope. Your curly brackets define the scope as everything inside the ‘switch’ statement. This means that you are left with a scope where a jump will be performed further into the code skipping the initialization. The correct way to handle this is to define a scope specific to that case statement and define your variable within it.

Reference:Why can’t variables be declared in a switch statement?

3.创建Group style的UITalbeView的顶端有很大一块空白。

解决办法:YouStoryboard.storyboard > YouViewController > Attributes inspector > Uncheck - Adjust scroll view insets

Reference:Why is there extra padding at the top of my UITableView with style UITableViewStyleGrouped in iOS7

继续阅读

(翻译)如何让didUpdateLocation兼容iOS 5和iOS 6

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 是CLLocationManagerDelegate protocol 中的一个常用方法,它让你的应用接收更新位置信息,当检测到任何位置变化。新的位置详情存储在 newLocation 中,它是一个 CLLocation.

当 iOS 6 启动,上述方法被废弃了,建议使用新版本方法 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 或简称locationManager:didUpdateLocations:.

这个快速教程的的目的是解释如何处理废弃方法,关于它什么是应该完成的以及你在哪里可以找出关于改变的更多细节。如果你想了解如何使用这个新的 locationManager:didUpdateLocations: 方法,看下didUpdateLocations tutorial,它解释了如何使用方法提供的NSArray.

当方法被废弃了

当 iOS 升级了(这会经常发生),Apple 找到新的或更有效方法。当这发生了,方法可以被标记为废弃并在如何使用的地方给出一个提示。具体到 CLLocationManagerDelegate,你可以看到文档中推荐了一个不同的方法。虽然你仍然可以使用废弃的方法,即使是在已经废弃的 iOS 版本中,Apple 在未来某个时间点也许会删除掉这个方法当 iOS 升级了。在那个时间点,你可能需要修改你的代码,提交到 Apple Store 通过审核流程。

继续阅读

Core Data 使用笔记

  1. Core Data是什么?
  2. 为什么使用Core Data?
  3. 如何使用Core Data?

Core Data是什么?

Core Data is a framework that you use to manage the model layer objects in your application. It provides generalized and automated solutions to common tasks associated with object life-cycle and object graph management, including persistence.

Core Data 是一个你在应用中用来管理模型层对象的框架。 它为关联对象生命周期和对象图管理的常见任务提供了通用的和自动的解决方案,包括持久化。

为什么使用Core Data?

There are a number of reasons why it may be appropriate for you to use Core Data. One of the simplest metrics is that, with Core Data, the amount of code you write to support the model layer of your application is typically 50% to 70% smaller as measured by lines of code. This is primarily due to the features listed above—the features Core Data provides are features you don’t have to implement yourself. Moreover they’re features you don’t have to test yourself, and in particular you don’t have to optimize yourself.

Core Data 的优点:

  • 允许你高效地从永久存储中获取模型对象和保存改变。
  • 提供一个记录模型对象改变的架构。它能让你自动支持undo和redo,维护对象之间的关系。
  • 允许你去维护模型对象不相交集合的编辑。不相交集合很有用,例如,让用户在一个可能会被丢弃的视图中编辑而不在另一个视图中显示的数据。
  • 允许你在任何时候只在内存中保持模型对象的一个子集。这对保持你应用尽可能地使用少的内存很有用。
  • 拥有数据存储版本和迁移的架构。该架构让你很容易把旧版本数据文件升级为现在的版本。
继续阅读