简体   繁体   English

创建UIImageView的子类

[英]Creating subclass of UIImageView

I havent found the option of UIImageView to choose from dropdown during creating objective-c class. 我还没有找到UIImageView的选项,可以在创建objective-c类时从下拉列表中进行选择。 I am using xcode 4.3. 我正在使用xcode 4.3。 I want to know arent we allowed to extend UIImageView class. 我想知道我们不允许扩展UIImageView类。 Or do i have to inherit UIView ? 或者我必须继承UIView? Any sample structure of custom UIImageView class would be helpful. 自定义UIImageView类的任何示例结构都会有所帮助。

Thanks 谢谢

I believe it's considered bad practice to subclass UIImageView. 我认为将子类化为UIImageView被认为是不好的做法。 If you take a look at this question, there are many examples of why you shouldn't do it: 如果你看看这个问题,有很多例子说明为什么你不应该这样做:

  • A part of the Gang of Fours design pattern philosophy is to "Favor 'object composition' over 'class inheritance'." Gang of Fours设计模式哲学的一部分是“赞成'对象构成'而不是'阶级继承'。” 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. 它是子类化UIView的原因是,例如,您应该在下载图像时显示UIActivityIndi​​cator。 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. 另外看一下这篇文章的评论,你会发现更多代码示例,还包括一些缓存和好东西。

However, it is possible to subclass UIImageView. 但是,可以将UIImageView子类化。 Just take a look at this question . 看看这个问题吧

A good alternative, suggested in the aforementioned question, is to use a category instead of subclassing UIImageView: 在上述问题中建议的一个好的替代方法是使用类别而不是子类化UIImageView:

  • I would suggest using an Objective-C "Category" instead of subclassing the UIImageView. 我建议使用Objective-C“Category”而不是子类化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. 当您使用类别时,您可以在原始类的任何实例上调用扩展函数(在您的情况下为UIImageView。这使您无需在任何可能想要使用新函数的地方有意识地使用子类。

Hope this helps! 希望这可以帮助!

You can subclass UIImageView. 您可以继承UIImageView。 Even though qegal's answer contains a lot of right stuff you should keep in mind, just put the cursor into the drop down and start typing the name of any class you want to inherit and it will start auto completion. 即使qegal的答案包含很多正确的东西你应该记住,只需将光标放入下拉列表并开始键入要继承的任何类的名称,它将开始自动完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM