我有UITableView
一个使用UITableViewCell
包含UITextField
.
我想在myMethod
单击()时调用一个方法(),UIControlEventTouchDown
并尝试通过执行以下操作将其连接到UITableView
委托方法中:cellForRowAtIndexPath
[tf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
单击时UITextField
,没有任何反应。
UITextField
我试图为以下之外的另一个人做同样的事情UITableView
:
[othertf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
当我点击othertf
该方法时,正如我所期望的那样。
我有点困惑,因为除了我换成tf
了othertf
.
下面是完整的代码cellForRowAtIndexPath
:
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *DetailCellIdentifier = @"DetailFieldView";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DetailCellIdentifier];
if (cell == nil) {
NSArray *cellObjects = [[NSBundle mainBundle] loadNibNamed:DetailCellIdentifier owner:self options:nil];
cell = (UITableViewCell*) [cellObjects objectAtIndex:0];
}
cell.selectionStyle = UITableViewCellSelectionStyleNone;
UITextField *tf = (UITextField *)[cell viewWithTag:2];
tf.text = @"some value";
[othertf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
[tf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];
return cell;
}
谁能发现我做错了什么?这可能很简单,因为我是 iOS 开发的新手。