从笔尖加载的UIButton的子类未正确初始化 [英] Subclasses of UIButton loaded from nibs are not initialized properly

查看:86
本文介绍了从笔尖加载的UIButton的子类未正确初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个非常简单的UIButton子类:

@interface MyButton : UIButton
@end

@implementation MyButton

- (id) initWithCoder:(NSCoder *)decoder
{
    if (!(self = [super initWithCoder:decoder]))
        return nil;

    NSLog(@"-[%@ initWithCoder:%@]", self, decoder);

    return self;
}

@end

在Interface Builder中,我添加了一个UIButton,将其按钮类型设置为 Rounded Rect ,并将其类标识设置为MyButton.

运行时,我有以下日志:

-[<MyButton: 0x5b23970; baseClass = UIButton; frame = (103 242; 114 37); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x5b23a90>> initWithCoder:<UINibDecoder: 0x6819200>]

但是该按钮不再是圆形矩形按钮.

在iOS 3.2和iOS 4上均可见.

这是一个错误还是我遗漏了一些明显的东西?

以编程方式创建MyButton的实例,不能接受,谢谢.

解决方案

通过编程,您可以使用+[UIButton buttonWithType:]实例化一个按钮,该按钮实际上是一个返回UIButton子类的工厂.因此,如果从UIButton派生,则实际上不是从圆形rect button类(UIRoundedRectButton)派生,而是从通用按钮类派生.但是您不能从UIRoundedRectButton AFAIK继承子类,因为它是内部类.

从UIButton派生似乎有问题,我看到很多人建议改用UIControl派生并自己实现绘图.

但是您可能会发现以下文章有帮助:

如何在UIButton子类中覆盖-drawrect?

http://www.cocoabuilder.com /archive/cocoa/284622-how-to-subclass-uibutton.html

http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

此外,我也不知道为什么要从UIButton派生,但是如果您要进行一些不涉及覆盖任何其他方法的自定义设置,则可以使用可以执行以下操作的事实可能会有所帮助:

- (id)initWithCoder:(NSCoder *)decoder {
   // Decode the frame
   CGRect decodedFrame = ...;
   [self release];
   self = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   [self setFrame:decodedFrame];
   // Do the custom setup to the button
   return self;
}

I have a very simple subclass of UIButton:

@interface MyButton : UIButton
@end

@implementation MyButton

- (id) initWithCoder:(NSCoder *)decoder
{
    if (!(self = [super initWithCoder:decoder]))
        return nil;

    NSLog(@"-[%@ initWithCoder:%@]", self, decoder);

    return self;
}

@end

In Interface Builder I add a UIButton, set its button type to Rounded Rect and its class identity to MyButton.

When running, I have the following log:

-[<MyButton: 0x5b23970; baseClass = UIButton; frame = (103 242; 114 37); opaque = NO; autoresize = RM+BM; layer = <CALayer: 0x5b23a90>> initWithCoder:<UINibDecoder: 0x6819200>]

but the button is not a round rect button anymore.

Observed on both iOS 3.2 and iOS 4.

Is this a bug or am I missing something obvious?

Create an instance of MyButton programmatically is not an acceptable answer, thanks.

解决方案

Programmatically, you instantiate a button with +[UIButton buttonWithType:] which is actually a factory that returns a subclass of UIButton. So if you derive from UIButton you actually don't derive from your round rect button class (UIRoundedRectButton) but from a generic button class. But you are not allowed to subclass from UIRoundedRectButton AFAIK since it's an internal class.

It seems to be problematic to derive from UIButton, I've seen a lot of people recommed to derive from UIControl instead and implement the drawing yourself.

But you might find these articles helpful:

How to override -drawrect in UIButton subclass?

http://www.cocoabuilder.com/archive/cocoa/284622-how-to-subclass-uibutton.html

http://www.cimgf.com/2010/01/28/fun-with-uibuttons-and-core-animation-layers/

Also, I don't know why you want to derive from UIButton, but if you want to do some customization that does not involve overwriting any other methods it might be helpful to use the fact that you can do something like this:

- (id)initWithCoder:(NSCoder *)decoder {
   // Decode the frame
   CGRect decodedFrame = ...;
   [self release];
   self = [UIButton buttonWithType:UIButtonTypeRoundedRect];
   [self setFrame:decodedFrame];
   // Do the custom setup to the button
   return self;
}

这篇关于从笔尖加载的UIButton的子类未正确初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆