【问题标题】:cant able to add click event in UIbutton无法在 UIbutton 中添加点击事件
【发布时间】:2012-01-23 07:39:50
【问题描述】:

我正在创建包含两个标签和一个按钮的自定义视图。我无法点击按钮。我也发送下面的代码。

.h 文件

#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 文件

#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];
}

【问题讨论】:

    标签: iphone objective-c uitableview uibutton


    【解决方案1】:

    创建目标时,您需要将目标设置为主视图而不是子视图:

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

    还有 - 你知道你在你的代码中做了什么吗?

    在函数 willTransitionToState 中,您要将 UIButtons 添加到按钮本身的子视图中吗? 这真的是你想做的吗?函数 willTransitionToState 甚至被调用了吗?

    【讨论】:

    • 是的,什么时候调用 willTransitionToState 我想添加按钮。
    【解决方案2】:

    请尝试在初始化时设置 UIButton Type 并将目标更改为 self。

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

    【讨论】:

      【解决方案3】:

      将目标更改为自我而不是子视图。

      UIButton *btn;
      
      
          btn=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, 64, 33)];
          [btn addTarget:self 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];
      

      【讨论】:

      • 是的。实际上我正在以编程方式创建它。
      【解决方案4】:
      newString = [oldString stringByReplacingOccurrencesOfString:@"-"withString:@" ' "];
      

      【讨论】:

        猜你喜欢
        • 2018-12-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-05-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-08-21
        相关资源
        最近更新 更多