gpt4 book ai didi

iphone - 创建 UIImageView 的子类

转载 作者:行者123 更新时间:2023-12-03 20:21:53 25 4
gpt4 key购买 nike

我还没有找到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com