简体   繁体   English

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

[英]How to use a custom UIButton category with Interface Builder?

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. 我正在使用UIButtons:默认状态为面朝下,选定状态为面朝上。 I need the buttons to have a boolean property that tells me if they've ever been flipped over. 我需要按钮具有boolean属性,该属性可以告诉我是否曾经被翻转过。 If not, it gets set to true (that way I'm not just drawing random cards on each flip). 如果不是,它将设置为true(这样,我不仅会在每次翻转中随机绘制卡片)。 I tried creating a category of UIButton called CardGameButton. 我尝试创建一个名为CardGameButton的UIButton类别。 In the .h file: 在.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? 我如何在IB中使用它? I have a bunch of UIButtons on a screen that I want to be CardGameButtons. 我在屏幕上有一堆希望成为CardGameButtons的UIButton。 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). 但是,当我尝试将视图控制器中对UIButton的调用切换为CardGameButton时,它告诉我CardGameButton不是类型(是的,我导入了文件)。 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". 当我尝试将情节提要中的UIButtons类切换为CardGameButtons时,控制台会告诉我它们是“未知类”。 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). 我尝试将UIButton子类化,但是没有用(我只需要将它们作为具有extra属性的RoundedRectButtons即可,并且由于您不能将RoundedRectButton子类化,因此它们无法正确显示)。 How do I make this work in IB? 如何在IB中进行这项工作?

You're treating CardGameButtons as though it's a subclass of UIButton when instead it's a category. 您将CardGameButtons视为UIButton的子类,而将其视为类别。 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. 您声明为UIButton都将具有discovered属性作为标准属性,但是您不能“创建” CardGameButtons因为它本身并不是一回事。

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 . 我建议您尝试UIButton并将其命名为CardGameButton Add the BOOL property to its interface. BOOL属性添加到其界面。 You can still make it a round rect button, by setting buttonType to UIButtonTypeRoundedRect "round rect buttons aren't a separate subclass." 通过将buttonType设置为UIButtonTypeRoundedRect “圆形 buttonType 按钮不是一个单独的子类” ,您仍然可以使其成为圆形buttonType 按钮。 . Then override 然后覆盖

- (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. 编辑 :正如您所说, buttonType实际上是readOnly ,因此我们不能使用它来绘制圆形rect按钮。 However, it turns out that it's not hard to draw it. 但是,事实证明绘制它并不难。

Check this tutorial . 检查本教程 It shows how to do it using CALayer property of UIButton and using UIBezeirPath . 它显示了如何使用UIButton CALayer属性和UIBezeirPath做到这UIBezeirPath

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

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