簡體   English   中英

如何初始化從UIButton繼承的自定義類?

[英]How do I initialize this custom class that inherits from UIButton?

我無法弄清楚我應該如何初始化這個自定義類,代碼不是我的,而且我是Obj-C的新手,所以我的速度很慢。

PFTintedButton.h

#import <UIKit/UIKit.h>
@class CALayer;

typedef enum
{
    PFTintedButtonRenderTypeStandard,
    PFTintedButtonRenderTypeCandy,
    PFTintedButtonRenderTypeOpal,
} PFTintedButtonRenderType;


@interface PFTintedButton : UIButton
{
@private
    UIColor * tint;
    CGFloat cornerRadius;
    PFTintedButtonRenderType renderType;

    UIImage * stretchImage;
    CALayer * glowLayer;

    UILabel * subLabel;

    BOOL customShadow;
    BOOL customTitleColor;
}

@property( nonatomic, retain ) UIColor * tint;
@property( nonatomic, assign ) CGFloat cornerRadius;
@property( nonatomic, assign ) PFTintedButtonRenderType renderType;
@property( nonatomic, readonly ) UILabel * subLabel;



@end

PFTintedButton.m摘錄

#import "PFTintedButton.h"
#import "PFDrawTools.h"
#import "UIColor+PFExtensions.h"
#import <QuartzCore/QuartzCore.h>

#define glowRadius      30

@interface PFTintedButton()
-(void) createBackgroundImage;
-(void) createGlowLayer;
@end


@implementation PFTintedButton

-(void) dealloc 
{
    SafeRelease( tint );
    SafeRelease( stretchImage );
    SafeRelease( subLabel );
    SafeRelease( glowLayer );

    [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(createGlowLayer) object: nil];
    [NSObject cancelPreviousPerformRequestsWithTarget: self selector: @selector(renderGlowLayerOnMainThread) object: nil];

    [super dealloc];
}

-(void) initCommon
{
    cornerRadius = 6;
}

-(id) initWithFrame: (CGRect) frame 
{
    if( self = [super initWithFrame: frame] ) 
    {
        [self initCommon];
    }

    return self;
}

-(id) initWithCoder: (NSCoder *) coder 
{
    if( self = [super initWithCoder: coder] ) 
    {
        // this is such a hack but there's no other way to determine if the title label's settings
        // have been modified in IB.
        NSDictionary * dic = [coder decodeObjectForKey: @"UIButtonStatefulContent"];
        NSObject * st = [dic objectForKey: [NSNumber numberWithInt: UIControlStateNormal]];
        NSString * bc = [st description];

        customTitleColor = [bc rangeOfString: @"TitleColor = UIDeviceRGBColorSpace 0.196078 0.309804 0.521569 1"].location == NSNotFound;
        customShadow = [bc rangeOfString: @"ShadowColor = UIDeviceRGBColorSpace 0.5 0.5 0.5 1"].location == NSNotFound;

        [self initCommon];

        //[super setBackgroundColor: [[UIColor redColor] colorWithAlphaComponent: .5]];
    }

    return self;
}

我只是包含了一小部分實現, 完整的源代碼可以在這里找到。 作為 Paul-Alexander 的這個存儲庫的一部分

我像這樣初始化它:

PFTintedButton* buttony = [PFTintedButton buttonWithType:UIButtonTypeCustom];
[buttony setTint:[UIColor redColor]];
[buttony setBackgroundColor:[UIColor redColor]];
[buttony setRenderType:PFTintedButtonRenderTypeCandy];
[buttony setTitle:@"Title for Button" forState:UIControlStateNormal];

它在PFDrawTools中打嗝,似乎色調變量為零。 那是因為我正在初始化錯誤嗎? 有人有這個成功嗎?

試試這個:

PFTintedButton* buttony = [[PFTintedButton alloc] initWithFrame:CGRectMake(x, y, width, height)];

它似乎顯式處理initWithFrame,我會嘗試。

PFTintedButton *buttony = [[[PFTintedButton alloc] initWithFrame:<some_frame>] autorelease];

你在PFTintedButton類中定義了buttonWithType: initialize方法嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM