I have a UIViewController class in which im trying to allocate a UIButton Class. Here is a sample code.
MyViewController.m
- (void)viewDidLoad
{
CGRect frame = CGRectMake(companybuttonxOffset, companybuttonyOffset, buttonWidth, buttonHeight);
CustomButton *customButton = [[CustomButton alloc]initWithFrame:frame];
[self.view addSubview:customButton];
[super viewDidLoad];
}
CustomButton.h
#import <UIKit/UIKit.h>
@interface CustomButton : UIButton {
}
@property (nonatomic, assign) NSInteger toggle;
- (void)buttonPressed: (id)sender;
@end
CustomButton.m
#import "CustomButton.h"
@implementation CustomButton
@synthesize toggle;
- (id) initWithFrame:(CGRect)frame
{
if (self = [super initWithFrame:frame]) {
//custom button code
[self addTarget: self action: @selector(buttonPressed:) forControlEvents: UIControlEventTouchUpInside];
}
return self;
}
- (void)buttonPressed: (id)sender
{
NSLog(@"buttonPressed !!!!!");
}
@end
Although the button is present in my viewcontroller, if i press the button, i keep getting this error - -[UIButton buttonPressed:]: unrecognized selector sent to instance 0xb1dca50
From what i understand after searching for a lot for an answer is that, initWithFrame never gets called when you subclass the button in IB. Instead i should use initWithCoder. Is this right ? If this is it, then i have no idea what NSCoder is, and how i can use it.
Im tired of searching for a solution for this, please help me out guys.