iOS - Adding Target/Action for UITextField Inside Custom UITableViewCell

早过忘川 提交于 2019-12-23 15:46:17

问题


I have UITableView which uses a custom UITableViewCell containing a UITextField.

I want to call a method (myMethod) when it is clicked (UIControlEventTouchDown) and attempted to wire this up inside the UITableView delegate method cellForRowAtIndexPath by doing the following:

[tf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];

When the UITextField is clicked, nothing happens.

I attempted to do the exact same thing for another UITextField outside of the UITableView:

[othertf addTarget:self action:@selector(myMethod) forControlEvents:UIControlEventTouchDown];

When I click on othertf the method is called as I would expect.

I'm a bit confused as the code is identical apart from I've swapped tf for othertf.

Below is the complete code for 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;
}

Can anyone spot what I'm doing wrong? It's probably something simple as I am new to iOS development.


回答1:


Use UITextField delegate Methods:

UITextField delegate

//Use this method insted of addTarget:
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {

    if (textField == tf) {
        [self myMethod];
        return NO;
    }

  return YES;
}

and dont forget to set the delegate to the textField:

 tf.delegate = self;



回答2:


When you touch a UITextField it swallows the touch itself and tells the keyboard to appear etc. Instead use the following events:

UIControlEventEditingDidBegin

UIControlEventEditingDidEnd

UIControlEventEditingChanged

Further improvements

Tags is a rather loose way of coupling your NIB and your code. Consider Making a DetailCell.h and DetailCell.m file, and set the root view in the NIB file to the DetailCell class, and create reference outlets for all the views you need to access in code, and action outlets for all the actions you need to react on.

This is done by CTRL clicking on a view in the interface builder, and dragging it into the DetailCell.h file. Interface Builder will now ask you wheter you want to create an action or a reference outlet (References are basically pointers, and actions are basically events)

dequeueReusableCellWithReuseIdentifier now calls initWithFrame on your DetailCell class every time is creates a new instance of it. It is now your job to load the NIB file in this function.

Alternatively you could register a NIB file for cell reuse on the tableview with

[tableview registerNib:[UINib nibWithNibName:DetailCellIdentifier bundle:nil] forCellWithReuseIdentifier: DetailCellIdentifier];

which the automatically loads the nib file every time a new cell is created. In >= iOS 5 (AFAIK) dequeueReusableCellWithReuseIdentifier never returns nil, but creates instances of the registered classes/nib files itself every time.



来源:https://stackoverflow.com/questions/13376009/ios-adding-target-action-for-uitextfield-inside-custom-uitableviewcell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!