there is this dark sorcery in ios is preventing my button being clicked. if i don't add button to the uitableviewcell, and i click the button, the event is triggered. but if the button is in uitableviewcell, it won't get triggered, it seems table i have sample code ready, if you guys can help me, please just create a simple single-view application in xcode, and just paste the following code
ios中有一个黑魔法阻止我的按钮被点击。如果我不向uitableviewcell添加按钮,然后单击按钮,事件就会被触发。但是如果按钮在uitableviewcell中,它不会被触发,看起来表我已经准备好了示例代码,如果你们可以帮助我,请在xcode中创建一个简单的单视图应用程序,然后粘贴下面的代码
//GRDViewController.h
/ / GRDViewController.h
#import <UIKit/UIKit.h>
@interface GRDViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong) UIView* container;
@property (nonatomic, strong) UIButton* button;
@property (nonatomic, strong) UITableView* tableView;
@property (nonatomic, strong) NSArray* arr;
@end
//GRDViewController.m
/ / GRDViewController.m
#import "GRDViewController.h"
@implementation GRDViewController
@synthesize button, container, arr, tableView;
- (void)_btnTapped {
NSLog(@"TAPPED");
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
self.button = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 100, 100)];
[self.button addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside];
[self.button setTitle:@"CLICK ME" forState:UIControlStateNormal];
self.arr = [[NSArray alloc] initWithObjects:@"123", @"456", @"678", nil];
self.container = [[UIView alloc]initWithFrame:self.button.frame];
[self.container addSubview:self.button];
[self.view addSubview:self.container];
//[self.view addSubview:self.button];
self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 100, 400, 400)];
self.tableView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
self.tableView.delegate = self;
self.tableView.dataSource = self;
[self.view addSubview:self.tableView];
self.tableView.backgroundColor = [UIColor redColor];
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath: (NSIndexPath *)indexPath {
static NSString *cellIdentifier = @"coolcell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
if (cell == nil) {
cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
//cell.accessoryType = UITableViewCellAccessoryNone;
UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
[btn addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlStateNormal];
[btn setTitle:[self.arr objectAtIndex:[indexPath row]] forState:UIControlStateNormal];
[cell.contentView addSubview:btn];
}
return cell;
}
- (NSInteger)tableView:(UITableView*)tableView numberOfRowsInSection:(NSInteger)section {
return [self.arr count];
}
@end
please help i feel this case should be common in ios, but nobody has solution for this???
请帮助我觉得这个案例在ios里应该是普遍的,但是没有人有解决的办法???
edit: when click on the button it prints NSLOG msg in the xcode console...so make sure...u r looking at the results there...
编辑:当点击按钮时,它会在xcode控制台打印NSLOG msg…所以要确保……你正在看那里的结果……
3 个解决方案
#1
7
you have not given any control event to button .Change it to UIControlEventTouchUpInside
from UIControlStateNormal
你没有给任何控件事件的按钮。改变它从UIControlEventTouchUpInside从uicontrolstat诺曼
#2
7
Instead of using UIControlStateNormal
for ControlEvents use UIControlEventTouchUpInside
like this
使用uicontroleventtouchupin而不是UIControlStateNormal,像这样
[btn addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside];
#3
1
Try doing this:
试着这样做:
[btn.titleLabel setUserInteractionEnabled: NO];
[btn。titleLabel setUserInteractionEnabled:没有);
#1
7
you have not given any control event to button .Change it to UIControlEventTouchUpInside
from UIControlStateNormal
你没有给任何控件事件的按钮。改变它从UIControlEventTouchUpInside从uicontrolstat诺曼
#2
7
Instead of using UIControlStateNormal
for ControlEvents use UIControlEventTouchUpInside
like this
使用uicontroleventtouchupin而不是UIControlStateNormal,像这样
[btn addTarget:self action:@selector(_btnTapped) forControlEvents:UIControlEventTouchUpInside];
#3
1
Try doing this:
试着这样做:
[btn.titleLabel setUserInteractionEnabled: NO];
[btn。titleLabel setUserInteractionEnabled:没有);