1
votes

I have a subclass of UITextField and want to be able to add it to the view in IB. What I did is added UITextField and changed its class to my subclass in Identity tab of Inspector. But I can only see UITextField on the view.

Code:

@interface ExtendedTextField : UITextField {
}

//implementation


@implementation ExtendedTextField

- (void)baseInit
{
    UIImage * curImage = [UIImage imageNamed:@"tfbg.png"];
    [self baseInitWithImage : curImage];
}

- (void)baseInitWithImage : (UIImage *) aImage
{
    aImage = [aImage stretchableImageWithLeftCapWidth:29 topCapHeight:29];
    self.background = aImage;
    self.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;

    UIView * curLeftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 10, self.frame.size.height)];

    self.leftView = curLeftView;
    self.rightView = curLeftView;
    [curLeftView release];
    self.leftViewMode = UITextFieldViewModeAlways;
    self.rightViewMode = UITextFieldViewModeAlways;
    [self setTextColor:[UIColor greenColor]];
}

- (void)awakeFromNib
{
    [super awakeFromNib];
    [self baseInit];
}

/*
- (id)initWithCoder:(NSCoder *)aDecoder
{
    self = [super initWithCoder:aDecoder];
    if (self) {
        [self baseInit];
    }
    return self;
}
*/

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

- (id)initWithFrame:(CGRect)frame withImage:(UIImage*)aImage
{
    self = [super initWithFrame:frame];
    if (self) {
        [self baseInitWithImage : aImage];
    }
    return self;
}

- (void)dealloc {
    [super dealloc];
}


@end

EDIT: I noticed several things:

-- if I put [super initWithFrame:...] instead of [super initWithCoder:...] inside

(id)initWithCoder:(NSCoder *)aDecoder

it works well.

-- Now I am using awakefromnib instead of initwithcoder and the only thing that get changed in textfield is textColor. Could someone explain why so?

Solution I needed to set border style to none. BS overlayed bg image.

1

1 Answers

1
votes

While showing your .xib in Xcode, reveal the right pane, select your view and in the third tab, change Classto whatever you want it to be.