iOS Programming: Problem extending UIButton class

I'm trying to extend UIButton class to add few methods but I'm having some problem when I try to init my object.

-(id)init{

UIImage* image = [UIImage imageNamed:@"button_background.png"];    
CGRect frame=  CGRectMake(100.0, 70.0, 45.0 ,45.0);

self.frame = frame;

[self setTitle:@"title" forState:UIControlStateNormal];
[self setBackgroundImage:image forState:UIControlStateNormal];
[self setImage:image forState:UIControlStateNormal];

NSLog(@"type: %d",self.buttonType);
NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f",frame.origin.x, frame.origin.y ,frame.size.width, frame.size.height);
NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f",self.frame.origin.x, self.frame.origin.y ,self.frame.size.width, self.frame.size.height);

if(self != nil){
    //
}
return self;

}

It seams to work fine, there are no warning or error during execution. But in the console appears some inconsistency between values of frame and self.frame, and of course the button does not appear on screen.

type: 0
x: 100.000000
y: 70.000000
width: 45.000000
height: 45.000000

x: 0.000000
y: 0.000000
width: 0.000000
height: -1.998576

Please help me I'm getting out of my mind! :\

Answers


When you are subclassing in Objective-C you must (1) override the designated initializer (2) call the designated initializer of the super-class. You do neither.

You should be doing something like this:

/** 
 * \brief Convenience class method to replace button method of UIButton
 */
+(id)myButton
{
    MyButton* myButton = [[MyButton alloc] 
                              initWithFrame:CGMakeRect(100.0, 70.0, 45.0, 45.0)];
    return [myButton autorelaase];
}

-(id)initWithFrame:(CGRect)f
{
    if(self = [super initWithFrame:f])
    {
        UIImage* image = [UIImage imageNamed:@"button_background.png"];    

        [self setTitle:@"title" forState:UIControlStateNormal];
        [self setBackgroundImage:image forState:UIControlStateNormal];
        [self setImage:image forState:UIControlStateNormal];

        NSLog(@"type: %d",self.buttonType);
        NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f", NSStringFromCGRect(frame));
        NSLog(@"x: %f\ny: %f\nwidth: %f\nheight: %f", NSStringFromCGRect(self.frame));
    }
    return self;
}

Hope this helps. Let me know if you need more info.


Need Your Help

Vertical navigation with sub menus with image up and down links for each level with active link highlight when on selected page

html css drop-down-menu menu submenu

I have the following css vertical navigation menu I have done. There are up and down images for the parent category 30px high for rollover and separate up and down images for the second and third l...

Which type of project should i create for web service

web-services guidelines

My system is built on Asp.net Mvc and now i want to create a new project in same project which will contain Web services So i want to know which type of project should i create Asp.net Web Applica...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.