Pass a method from my custom cells class to the main class

前端 未结 6 1724
余生分开走
余生分开走 2020-12-22 06:04

I have a custom cell. In it is a UITextField linked to customCell.m. I\'m trying to pass the text from the textField to mainVC.m

6条回答
  •  死守一世寂寞
    2020-12-22 06:58

    There is a much easier way of doing this.

    This is assuming you are doing this all programatically and that you have your custom cell set up correctly with the textField working.

    First you need to make your textField accessible from outside the custom cell. You do this in the normal way of putting it in the header file as a property:

    customCell.h

    @property (weak, nonatomic) IBOutlet UITextField * customTextField;
    

    Make sure you allocate it in the custom cell XIB.

    Now we have a pointer to our custom textField when we access the cell in cellForRowAtIndex of your main view controller:

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
        //Adding the cell - remember to add the identifier to the xib file
        BCustomCell * cell = [tableView dequeueReusableCellWithIdentifier:bCustomCellIdentifier];
    
        // We can now access our cell's textField
        cell.textLabel.text = cell.customTextField.text;
    
        return cell;
    }
    

    Hopefully this helps you out.

提交回复
热议问题