iPhone:Override UIButton buttonWithType返回子类
我希望能够创建一个超大响应区域的UIButton。我知道这样做的一种方法是重写子类中的hitTest方法,但是首先我如何实例化我的自定义按钮对象呢iPhone:Override UIButton buttonWithType返回子类,iphone,uibutton,subclass,Iphone,Uibutton,Subclass,我希望能够创建一个超大响应区域的UIButton。我知道这样做的一种方法是重写子类中的hitTest方法,但是首先我如何实例化我的自定义按钮对象呢 [OversizedButton buttonWithType: UIButtonTypeDetailDisclosure]; 无法开箱即用,因为buttonWithType返回UIButton,而不是超大按钮 因此,似乎我也需要重写buttonWithType方法。有人知道怎么做吗 @implementation OversizedButton
[OversizedButton buttonWithType: UIButtonTypeDetailDisclosure];
无法开箱即用,因为buttonWithType返回UIButton,而不是超大按钮
因此,似乎我也需要重写buttonWithType方法。有人知道怎么做吗
@implementation OversizedButton
+ (id)buttonWithType:(UIButtonType)buttonType
{
// Construct and return an OversizedButton rather than a UIButton
// Needs to handle special types such as UIButtonTypeDetailDisclosure
// I NEED TO KNOW HOW TO DO THIS PART
}
- (UIView *)hitTest:(CGPoint)point withEvent:(UIEvent *)event
{
// Return results if touch event was in oversized region
// I ALREADY KNOW HOW TO DO THIS PART
}
@end
或者,我可以使用alloc/initWithFrame创建按钮。但是buttonType属性是只读的,那么如何创建自定义按钮类型呢
注意:我知道还有其他方法可以做到这一点,比如在可见按钮后面有一个不可见的按钮。我不喜欢这种方法,我宁愿避免它。对上述方法的任何帮助都将非常有用。谢谢UIKit中没有使用
按钮类型
属性,在您的代码中,您可以随时检查-iskindof class:
,因此在我看来,用类型:覆盖此属性的+按钮是毫无意义的。只需使用
return [[[OversizedButton alloc] initWithFrame:...] autorelease];
(您可以使用未记录的方法覆盖按钮类型\u setButtonType:
)UIButton按钮类型:
返回一个UIButton
或一个UIRoundedRectButton
,具体取决于类型
参数的值
由于UIButton
没有提供initWithType:
方法,我认为尝试覆盖buttonWithType:
是危险的
我建议您改为子类UIControl
。然后,您可以将一个按钮作为子视图添加到控件中,并截取hitTest:withEvent:
我是这样做的:
+ (instancetype)customButtonWithCustomArgument:(id)customValue {
XYZCustomButtom *customButton = [super buttonWithType:UIButtonTypeSystem];
customButton.customProperty = customValue;
[customButton customFunctionality];
return customButton;
}
也适用于其他类型,UIButtonTypeSystem
只是一个例子。你是认真建议我在需要苹果批准的商业应用程序中使用未记录的方法吗?@Ama:我建议你不要为buttonType
属性操心。但是如果你必须的话,唯一可以改变它的方法就是通过未记录的方法。好吧,我需要显示UIButtonTypedTailDisclosure按钮,所以“不打扰”是不起作用的。我是否应该假设据您所知,没有办法重写buttonWithType方法?谢谢,我正在试验,因为它确实看起来另一个不实用。但这看起来非常不雅。谢谢