Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

iphone - cant able to add click event in UIbutton

I am creating custom view which include two labels and One button. I cant get the button click. I am also sending the code which is below.

.h File

#import <UIKit/UIKit.h>


@interface customDeleteButton : UITableViewCell {

    IBOutlet UILabel *lbl;
    IBOutlet UILabel *lbl1;
 }
 @property(nonatomic,retain)IBOutlet UILabel *lbl;
 @property(nonatomic,retain)IBOutlet UILabel *lbl1;
 -(IBAction)btnClick:(id)sender;
 @end

.m File

#import "customDeleteButton.h"


@implementation customDeleteButton
@synthesize lbl,lbl1;
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {

self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
if (self) {
    // Initialization code.
    lbl = [[UILabel alloc] initWithFrame:CGRectMake(50, 10, 200, 20)];
    lbl1 = [[UILabel alloc] initWithFrame:CGRectMake(260, 10, 200, 20)];
    [self addSubview:lbl];
    [self addSubview:lbl1];

}
return self;
}

- (void)willTransitionToState:(UITableViewCellStateMask)state{

[super willTransitionToState:state];


if ((state & UITableViewCellStateShowingDeleteConfirmationMask) == UITableViewCellStateShowingDeleteConfirmationMask) {


for (UIView *subview in self.subviews) {

    if ([NSStringFromClass([subview class]) isEqualToString:@"UITableViewCellDeleteConfirmationControl"]) {             

        UIButton *btn;


        btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
        [btn addTarget:subview action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [btn setImage:[UIImage imageNamed:@"delete.png"] forState:UIControlStateNormal];
        btn.userInteractionEnabled=YES;
        btn.frame=CGRectMake(0,0,64,33);
        //UIImageView *deleteBtn = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
        //[deleteBtn setImage:[UIImage imageNamed:@"delete.png"]];
        [[subview.subviews objectAtIndex:0] addSubview:btn];
        //[deleteBtn release];

    }       

}
  } 

}
 -(void)btnClick:(id)sender
{
    NSLog(@"HTllo");
}


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

     [super setSelected:selected animated:animated];

// Configure the view for the selected state.
 }


- (void)dealloc {
    [super dealloc];
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You need to set the target to your main view not the subviews, when you create it:

[btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

And also - do you realize what you're doing in your code?

In a function willTransitionToState you are adding UIButtons to subviews of the button itself? Is this really what you want to do? Is the function willTransitionToState even called?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...