I'm coding a simple card game for my younger sister on a whim. I'm using UIButtons: default state is face down, selected state is face up. I need the buttons to have a boolean property that tells me if they've ever been flipped over. If not, it gets set to true (that way I'm not just drawing random cards on each flip). I tried creating a category of UIButton called CardGameButton. In the .h file:
我一时兴起为我的妹妹编写一个简单的纸牌游戏。我正在使用UIButtons:默认状态是面朝下,选择状态正面朝上。我需要按钮有一个布尔属性,告诉我它们是否曾被翻过来。如果没有,它被设置为true(这样我不只是在每次翻转时绘制随机卡)。我尝试创建一个名为CardGameButton的UIButton类。在.h文件中:
@interface UIButton (CardGameButton)
@property (nonatomic) BOOL discovered;
@end
In the .m file:
在.m文件中:
@implementation UIButton (CardGameButton)
@dynamic discovered;
@end
This is really all I need. How do I use this in IB? I have a bunch of UIButtons on a screen that I want to be CardGameButtons. But when I try to switch my calls to UIButton in my view controller to CardGameButton, it tells me CardGameButton isn't a type (yes I imported the file). And when I try to switch the class of the UIButtons in the storyboard to CardGameButtons, the console tells me that they're an "unknown class". I tried subclassing UIButton, but that didn't work (I just need them to be RoundedRectButtons with the extra property, and since you can't subclass RoundedRectButton they wouldn't display properly). How do I make this work in IB?
这就是我真正需要的。我如何在IB中使用它?我在屏幕上有一堆UIButtons,我想成为CardGameButtons。但是当我尝试将我的视图控制器中的UIButton调用切换到CardGameButton时,它告诉我CardGameButton不是一个类型(是的,我导入了文件)。当我尝试将故事板中的UIButtons类转换为CardGameButtons时,控制台告诉我它们是一个“未知类”。我尝试了子类化UIButton,但是这不起作用(我只需要它们是带有额外属性的RoundedRectButtons,因为你不能将RoundedRectButton子类化,它们将无法正确显示)。我如何在IB中完成这项工作?
2 个解决方案
#1
1
You're treating CardGameButtons
as though it's a subclass of UIButton when instead it's a category. Anything you declare as UIButton
will have the discovered
property as standard, but you can't 'create' a CardGameButtons
as it's not a thing in itself.
您将CardGameButtons视为UIButton的子类,而不是它是一个类别。你声明为UIButton的任何东西都会将已发现的属性作为标准,但是你不能“创建”一个CardGameButtons,因为它本身并不是一件事。
#2
0
Categories are not types. They're just used to add some extended behavior to the class.
类别不是类型。它们只是用来为类添加一些扩展行为。
I suggest that you try subclassing UIButton
and call it CardGameButton
. Add the BOOL
property to its interface. You can still make it a round rect button, by setting buttonType
to UIButtonTypeRoundedRect
"round rect buttons aren't a separate subclass.". Then override
我建议您尝试继承UIButton并将其称为CardGameButton。将BOOL属性添加到其接口。您仍然可以将它设置为圆形矩形按钮,方法是将buttonType设置为UIButtonTypeRoundedRect“圆形矩形按钮不是单独的子类。”。然后覆盖
- (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];
}
You can add a UIButton
to your view from the IB, and then its class to your created subclass and set its type to round rect.
您可以从IB中将UIButton添加到视图中,然后将其类添加到创建的子类中,并将其类型设置为round rect。
Edit: As you stated, buttonType
is actually readOnly
, so we can't use that to draw a round rect button. However, it turns out that it's not hard to draw it.
编辑:如你所说,buttonType实际上是readOnly,所以我们不能用它来绘制一个圆形的矩形按钮。但事实证明,绘制它并不难。
Check this tutorial. It shows how to do it using CALayer
property of UIButton
and using UIBezeirPath
.
查看本教程。它显示了如何使用UIButton的CALayer属性并使用UIBezeirPath来完成它。
#1
1
You're treating CardGameButtons
as though it's a subclass of UIButton when instead it's a category. Anything you declare as UIButton
will have the discovered
property as standard, but you can't 'create' a CardGameButtons
as it's not a thing in itself.
您将CardGameButtons视为UIButton的子类,而不是它是一个类别。你声明为UIButton的任何东西都会将已发现的属性作为标准,但是你不能“创建”一个CardGameButtons,因为它本身并不是一件事。
#2
0
Categories are not types. They're just used to add some extended behavior to the class.
类别不是类型。它们只是用来为类添加一些扩展行为。
I suggest that you try subclassing UIButton
and call it CardGameButton
. Add the BOOL
property to its interface. You can still make it a round rect button, by setting buttonType
to UIButtonTypeRoundedRect
"round rect buttons aren't a separate subclass.". Then override
我建议您尝试继承UIButton并将其称为CardGameButton。将BOOL属性添加到其接口。您仍然可以将它设置为圆形矩形按钮,方法是将buttonType设置为UIButtonTypeRoundedRect“圆形矩形按钮不是单独的子类。”。然后覆盖
- (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];
}
You can add a UIButton
to your view from the IB, and then its class to your created subclass and set its type to round rect.
您可以从IB中将UIButton添加到视图中,然后将其类添加到创建的子类中,并将其类型设置为round rect。
Edit: As you stated, buttonType
is actually readOnly
, so we can't use that to draw a round rect button. However, it turns out that it's not hard to draw it.
编辑:如你所说,buttonType实际上是readOnly,所以我们不能用它来绘制一个圆形的矩形按钮。但事实证明,绘制它并不难。
Check this tutorial. It shows how to do it using CALayer
property of UIButton
and using UIBezeirPath
.
查看本教程。它显示了如何使用UIButton的CALayer属性并使用UIBezeirPath来完成它。