开发者

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

开发者 https://www.devze.com 2023-03-14 10:20 出处:网络
I can\'t figure out how I\'m supposed to initialize this custom class, the code isn\'t mine and I\'m fairly new to Obj-C, so I\'m slow on the uptake.

I can't figure out how I'm supposed to initialize this custom class, the code isn't mine and I'm fairly new to Obj-C, so I'm slow on the uptake.

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 excerpt

#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;
}

I just included a small bit of the implementation, the full source can be found here. As a part of this repository by Paul-Alexander.

I initialize it like this:

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

And it hiccups in PFDrawTools, it seems the tint variable is nil. So is it because I am initializing it incorrectly? Has anyone had success with this?


It seems to explicitly handle initWithFrame, I would try that.

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


Try this instead:

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


Have you defined buttonWithType: initialize method in PFTintedButton class?

0

精彩评论

暂无评论...
验证码 换一张
取 消