2012-01-23 57 views
-1

我正在創建包含兩個標籤和一個按鈕的自定義視圖。我不能得到按鈕點擊。我也發送下面的代碼。無法在UIbutton中添加點擊事件

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

如果有人有這個比請幫我的任何解決方案。我需要它緊急。

回答

0

將目標更改爲自我而不是子視圖。

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]; 
+0

感謝名單,但我想它。它不工作。 – Akash

+0

是你點擊按鈕? –

+0

是的。其實我是以編程方式創建它。 – Akash

2

您需要在目標設置爲您的主視圖沒有子視圖,當您創建它:

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

而且還 - 你有沒有意識到你在你的代碼做什麼?

在函數willTransitionToState中,您將UIButtons添加到按鈕本身的子視圖中? 這真的是你想要做的嗎?函數willTransToToState甚至調用?

+0

它不工作。 – Akash

+0

檢查我的更新回答 –

+0

yes當willTransitionToState在那個時候調用我想添加按鈕。 – Akash

1

請在初始化時嘗試設置UIButton類型,並將目標更改爲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]; 
-1
newString = [oldString stringByReplacingOccurrencesOfString:@"-"withString:@" ' "];