ios 为什么我不应该继承 UIButton?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13202161/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 21:04:38  来源:igfitidea点击:

Why shouldn't I subclass a UIButton?

objective-ciosuibuttonuikitsubclass

提问by SirRupertIII

I've asked a few questions on stack overflow about subclassing a UIButton, and a couple of people have informed me that I shouldn't subclass a UIButton.

我问了几个关于子类化 a 的堆栈溢出问题UIButton,有几个人告诉我我不应该子类化 a UIButton

What are the negatives of subclassing a UIButton? And I know it's vague, but what are other alternatives to subclassing a UIButton?

子类化 a 的缺点是UIButton什么?而且我知道这很模糊,但是子类化 a 的其他替代方法是UIButton什么?

回答by Chris Trahey

The Cocoa frameworks take the approach that the Object Composition pattern is more appropriate than traditional class hierarchy.

Cocoa 框架采用的方法是对象组合模式比传统的类层次结构更合适。

In general, this means that there is likely to be a property on UIButton where you can set another object to handle various aspects of the button. This is the preferred way to "customize" how your button works.

通常,这意味着 UIButton 上可能有一个属性,您可以在其中设置另一个对象来处理按钮的各个方面。这是“自定义”按钮工作方式的首选方式。

One of the main reasons for this pattern is that many library components create buttons and don't know that you want them to create instances of your subclass.

这种模式的主要原因之一是许多库组件创建按钮并且不知道您希望它们创建子类的实例。

edit, your own factory method

编辑,你自己的工厂方法

I noticed your comment above about saving time when you have the same button config across many buttons in your app. This is a great time to use the Factory Method design pattern, and in Objective-C you can implement it with a Categoryso it's available directly on UIButton.

我注意到您在上面关于节省时间的评论,当您的应用程序中的许多按钮具有相同的按钮配置时。这是使用工厂方法设计模式的好时机,在 Objective-C 中,您可以使用 Category实现它,因此它可以直接在 UIButton 上使用。

@interface UIButton ( MyCompanyFactory )
+(UIButton *) buttonWithMyCompanyStyles;
@end
@implementation UIButton
+(UIButton *) buttonWithMyCompanyStyles {
    UIButton *theButton = [UIButton buttonWithType:UIButtonTypeCustom];
    // [theButton set...
    return theButton;
}
@end

回答by justin

It's because UIButtonis kind of special in that there are a few complexities/subtleties/restrictions (i.e. additional overrides for you to define, notably +buttonWithType:) required in order for it to work as expected. It's more than the usual -initWithFrame:(and -initWithCoder:, if used in XIBs). IDK why the framework authors allowed those details to leak out into our domain, but it's something that must be dealt with by us now. The restriction is that your implementation must not depend on (i.e. extend) preset system button styles; You must assume UIButtonTypeCustomas your starting point for a UIButtonsubclass.

这是因为UIButton它有点特殊,因为它需要一些复杂性/微妙之处/限制(即您需要定义的额外覆盖,特别是+buttonWithType:)才能按预期工作。它比平常更多-initWithFrame:(并且-initWithCoder:,如果在 XIB 中使用)。IDK 为什么框架作者允许这些细节泄露到我们的领域,但这是我们现在必须处理的事情。限制是您的实现不得依赖(即扩展)预设系统按钮样式;您必须假设UIButtonTypeCustom您是创建UIButton子类的起点。



On implementing a subclass of UIButton

关于实现子类 UIButton

回答by James Matthew Mudgett

If you are just looking for something more lightweight with your own 'subviews' you should instead be subclassing UIControl. UIButton subclasses UIControl and can handle events, like:

如果你只是想用你自己的“子视图”寻找更轻量级的东西,你应该继承 UIControl。UIButton 是 UIControl 的子类并且可以处理事件,例如:

[mySubclassedButtonFromUIControl addTarget:self action:@selector(_doSomething:) forControlEvents:UIControlEventTouchUpInside];

UIControl subclasses UIView so you can cleanly layoutSubviews on any views contained by your UIControl subclass and avoid unnecessary views that come with UIButton. In essence you are just creating your own 'UIButton' but you avoid having to work around behavior and functionality you don't really want or need.

UIControl 是 UIView 的子类,因此您可以在 UIControl 子类包含的任何视图上干净地 layoutSubviews,并避免 UIButton 附带的不必要的视图。从本质上讲,您只是在创建自己的“UIButton”,但您不必处理您并不真正想要或不需要的行为和功能。