iphone - 创建 UIImageView 的子类

标签 iphone uiimageview

我还没有找到 UIImageView 在创建 Objective-C 类时从下拉列表中进行选择的选项。我使用的是 xcode 4.3。我想知道我们是否允许扩展 UIImageView 类。

或者我必须继承 UIView 吗?自定义 UIImageView 类的任何示例结构都会有所帮助。

最佳答案

我认为子类化 UIImageView 被认为是不好的做法。如果你看一下this问题,有很多例子说明为什么你不应该这样做:

  • A part of the Gang of Fours design pattern philosophy is to "Favor 'object composition' over 'class inheritance'." This reduces the tight coupling between ojects. Then changing one class will have less impact on the other classes in the system. This makes changes easier, resulting in a more stable, easy to maintain system.

  • The reason it is subclassing UIView is so that you should, for example, display a UIActivityIndicator while the image is being downloaded. They do not show this in their example but I have used this code and it is really good. Also look at the comments for this post you will find more code examples, also including some caching and nice stuff.

但是,可以子类化 UIImageView。看看这个question .

上述问题中建议的一个不错的选择是使用类别而不是子类化 UIImageView:

  • I would suggest using an Objective-C "Category" instead of subclassing the UIImageView. As long as you don't have to add any member variables, a Category is a better solution. When you use a category you can call your extended functions on any instance of the original class (in your case UIImageView. That removes the need for you to consciously use your subclass anywhere you might want to use your new functions.

希望这有帮助!

关于iphone - 创建 UIImageView 的子类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11719409/

相关文章:

swift - 快速覆盖类的属性

iphone - 将 UIImageView 添加到 UIView

ios - 修改 Collection View 单元格内容的图层属性

objective-c - iPhone 6 上的 native 分辨率,适用于模拟器,但不适用于设备

iphone - 如何按字母顺序重新排列 NSArray 的元素(NSString)?

ios - 解析 - 从关系中获取对象

ios - 剃掉 UIImageView 的一侧?

Facebook 请求工作需要 iPhone App Store ID?

iphone - AVAudioPlayer 在 iPhone 5 上不工作

swift - TableView 在新 Controller 上显示不同的图像