sohel14_cse_ju
sohel14_cse_ju

Reputation: 2521

TableView CustomCell UIButton ISSUE

I am using a customCell for my tableView.The CustomCell Contains UIButton.And i want to assign an action with this button.

BeginingCell.h holds declination for the UIButton as :

#import <UIKit/UIKit.h>
@interface BeginingCell : UITableViewCell {
    IBOutlet UIButton *ansBtn1; 
}

@property(nonatomic,retain)IBOutlet UIButton *ansBtn1;

@end

And the implementation is :

//
//  BeginingCell.m
//  customCells
//

#import "BeginingCell.h"    

@implementation BeginingCell

///########### Synthesize ///###########

@synthesize ansBtn1;

///########### Factory ///###########

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

    }
    return self;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {

    [super setSelected:selected animated:animated];

    // Configure the view for the selected state.
}

///########### End Factory ///###########

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

I am using this CustomCell in a tableView.I want a response from the button Click.How can i assign a button action for my customCell Button?

Upvotes: 1

Views: 216

Answers (3)

Jhaliya - Praveen Sharma
Jhaliya - Praveen Sharma

Reputation: 31730

You can programmatically add an action to the UIButton using the addTarget:action:forControlEvents: method (a method of UIControl, of which UIButton is a subclass).

So your implementation would look something like:

[ansBtn1 addTarget:self
             action:@selector(btnAction)
   forControlEvents:UIControlEventTouchUpInside];


- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {

          [ansBtn1 addTarget:self
             action:@selector(btnAction:)
          forControlEvents:UIControlEventTouchUpInside];
    }
    return self;
}
//Buttons Action
-(void) btnAction:(id) sender
{

}

Upvotes: 0

Sabobin
Sabobin

Reputation: 4276

Rather than making your button an IBOutlet and trying to wrire it to ur IBAction, just wire it manualy in -(void)viewDidLoad like so:

[ansBtn1 addTarget:self action: @selector(myActionMethod:) 
        forControlEvents:UIControlEventTouchUpInside];

Upvotes: 0

visakh7
visakh7

Reputation: 26400

myButton = [UIButton buttonWithType:UIButtonTypeCustom];
[myButton addTarget:self
             action:@selector(myButtonAction:)
   forControlEvents:UIControlEventTouchUpInside];
[myButton setTitle:@"Action" forState:UIControlStateNormal];
myButton.frame = CGRectMake(buttonFrame);
[self addSubview:myButton];

Upvotes: 1

Related Questions