Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/100.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在Interface Builder中使用自定义UIButton类别?_Ios_Objective C_Uibutton_Interface Builder - Fatal编程技术网

Ios 如何在Interface Builder中使用自定义UIButton类别?

Ios 如何在Interface Builder中使用自定义UIButton类别?,ios,objective-c,uibutton,interface-builder,Ios,Objective C,Uibutton,Interface Builder,我一时兴起,正在为我妹妹编写一个简单的纸牌游戏。我正在使用UIButtons:默认状态是面朝下,选择的状态是面朝上。我需要按钮有一个布尔属性,告诉我它们是否被翻转过。如果不是,则设置为true(这样我就不只是在每次翻转时随机抽牌)。我尝试创建一个名为CardGameButton的UIButton类别。在.h文件中: @interface UIButton (CardGameButton) @property (nonatomic) BOOL discovered; @end 在.m文件中: @

我一时兴起,正在为我妹妹编写一个简单的纸牌游戏。我正在使用UIButtons:默认状态是面朝下,选择的状态是面朝上。我需要按钮有一个布尔属性,告诉我它们是否被翻转过。如果不是,则设置为true(这样我就不只是在每次翻转时随机抽牌)。我尝试创建一个名为CardGameButton的UIButton类别。在.h文件中:

@interface UIButton (CardGameButton)
@property (nonatomic) BOOL discovered;
@end
在.m文件中:

@implementation UIButton (CardGameButton)
@dynamic discovered;
@end

这是我真正需要的。我如何在IB中使用它?我在屏幕上有一堆UIButton,我想成为CardGameButtons。但当我尝试将视图控制器中对UIButton的调用切换到CardGameButton时,它告诉我CardGameButton不是一种类型(是的,我导入了该文件)。当我尝试将故事板中的UIButtons类切换到CardGameButtons时,控制台告诉我它们是“未知类”。我尝试子类化UIButton,但这不起作用(我只需要它们是带有额外属性的RoundedRectButtons,因为您不能子类化RoundedRectButton,所以它们不会正确显示)。如何在IB中实现此功能?

您将
CardGameButtons
视为UIButton的子类,而不是类别。任何声明为
UIButton
的内容都将以
发现的
属性作为标准,但您不能“创建”一个
CardGameButtons
,因为它本身不是一个东西。

您将
CardGameButtons
视为UIButton的子类,而不是一个类别。任何声明为
UIButton
的内容都会将
发现的
属性作为标准,但您不能“创建”一个
CardGameButtons
,因为它本身不是一件事。

类别不是类型。它们只是用来向类中添加一些扩展行为

我建议您尝试子类化
UIButton
,并将其命名为
CardGameButton
。将
BOOL
属性添加到其接口。通过将
buttonType
设置为
UIButtontyPeroundRect
“圆形矩形按钮不是一个单独的子类”,您仍然可以将其设置为圆形矩形按钮。然后覆盖

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    if(event.type == UIEventTypeTouches){
        self.discovered = YES; // or handle the logic as you want here
    }
    [super sendAction:action to:target forEvent:event];
}
您可以从IB向视图添加一个
UIButton
,然后将其类添加到您创建的子类中,并将其类型设置为round rect


编辑:如您所述,
按钮类型实际上是
只读的,因此我们不能用它来绘制圆形矩形按钮。然而,事实证明,画出来并不难


检查。它显示了如何使用
UIButton
CALayer
属性以及使用
UIBezeirPath

类别不是类型。它们只是用来向类中添加一些扩展行为

我建议您尝试子类化
UIButton
,并将其命名为
CardGameButton
。将
BOOL
属性添加到其接口。通过将
buttonType
设置为
UIButtontyPeroundRect
“圆形矩形按钮不是一个单独的子类”,您仍然可以将其设置为圆形矩形按钮。然后覆盖

- (void)sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    if(event.type == UIEventTypeTouches){
        self.discovered = YES; // or handle the logic as you want here
    }
    [super sendAction:action to:target forEvent:event];
}
您可以从IB向视图添加一个
UIButton
,然后将其类添加到您创建的子类中,并将其类型设置为round rect


编辑:如您所述,
按钮类型实际上是
只读的,因此我们不能用它来绘制圆形矩形按钮。然而,事实证明,画出来并不难


检查。它展示了如何使用
UIButton
CALayer
属性和
UIBezeirPath

进行操作,但类别无法添加任何存储,因此您无法添加任何属性(至少是具有支持ivar的属性)。但是类别无法添加任何存储,因此您无法添加任何属性(至少是具有支持ivar的属性).由于类别不能有属性,我建议您只使用自定义按钮,并根据需要绘制任何图形,或使用带有点击手势识别器的UIImageView作为“按钮”。由于类别不能有属性,我建议您只使用自定义按钮,并根据需要绘制任何图形,或者使用带有点击手势识别器的UIImageView作为“按钮”。buttonType是只读属性。我做错什么了吗
-(id)initWithCoder:(NSCoder*)aDecoder{if(self=[super initWithCoder:aDecoder]){self.buttonype=uibuttonyperoundrect;}}}
事实上,你是对的<代码>按钮类型为只读。看来你得自己画了。检查我的edit.buttonType是否为只读属性。我做错什么了吗
-(id)initWithCoder:(NSCoder*)aDecoder{if(self=[super initWithCoder:aDecoder]){self.buttonype=uibuttonyperoundrect;}}}
事实上,你是对的<代码>按钮类型为只读。看来你得自己画了。检查我的编辑。