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

前端 未结 6 1694
余生分开走
余生分开走 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:40

    How about creating a @class CustomCell; and @propety (strong, nonatomic) CustomCell *customCell in MainVC. Then you can access it by self.customCell.textField.text.

    0 讨论(0)
  • 2020-12-22 06:46

    As per my understanding I think you looking for this approach, Let me know if I am wrong here.

    In did select row at index path, First you need to find the your custom cell and then the textfield you required. Like this:

        - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
        [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
        customCell* cell=(customCell*)[tableView cellForRowAtIndexPath:indexPath.Row];
        NSString* yourTextFieldText=cell.textField.text;
        mainVC* objmainVC=[mainVC alloc]init];
        objmainVC.yourTextFieldText=yourTextFieldText;
        [self.navigationController pushViewController:objmainVC animated:YES];
    
    }
    

    Now you can use yourTextFieldText to mainVC controller as well.

    0 讨论(0)
  • 2020-12-22 06:48

    Mike, Normal pattern for data passing is to use properties and delegates. If you want to pass back information to a view controller, use a delegate. I am not sure if you are talking about a UItableview cell or a collection cell when you say a custom cell. Both uitableview and collection view has plenty of ways to pass information back to the view controller.

    0 讨论(0)
  • 2020-12-22 06:54

    What you need to do is:

    1. Define a protocol
    2. Add a property of this protocol for your CustomCell
    3. Implement the protocol in your MainVC

    1. Define a protocol

    We normally put the definition of the protocol in the header file (in this case CustomCell.h).

     // define the protocol for the delegate
     @protocol CustomCellDelegate 
     // define protocol functions that can be used in any class using this delegate
     -(void)customCell:(CustomCell *)customCell passText:(NSString *)text;
     @end
    

    2. Add a property of this protocol for your CustomCell

    And add this to your CustomCell between @interface CustomCell and @end.

    @property (nonatomic, weak) id<CustomCellDelegate> delegate; 
    

    3. Implement the protocol in your MainVC

    In your MainVC, implement the delegate function as the following.

    @interface MainCV<CustomCellDelegate>
    @end
    
    @implementation MainVC 
    -(void)customCell:(CustomCell *)customCell passText:(NSString *)text
    {
         // Do whatever you want with text here
    }
    @end
    

    The following shows how you use the protocol above.

    1. Set the delegate when create CustomCell in your MainCV. Something like the following,

      CustomCell *cell = ... allocation and initialization
      cell.delegate = self; // self is mainVC
      
    2. Whenever you need to pass the data NSString in your customCell, call the following:

      [self.delegate customCell:self passText:self.myTextField.text]; // self is customCell
      
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-22 06:59

    Yes u can do this with help of delegates for example,

    in the custom cell, i took one button and textfield and i defined a protocol like below

    //in CustomCell.h file
    @class CustomCell;
    @protocol CellDelegate <NSObject> //protocol defination
    - (void)whenDoneButtonClicked:(CustomCell *)cell; //i am passing the cell this would be the good to get whole information of a cell for example apart from properties u can get index path ..
    @end
    
    
    @interface CustomCell : UITableViewCell <UIWebViewDelegate>
    @property (weak, nonatomic) IBOutlet UIButton *aButton;
    @property (weak, nonatomic) IBOutlet UITextField *aTextField;
    
    @property (nonatomic, assign) id<CellDelegate> delegate; //declare a delegate 
    
    @end
    

    in CustomCell.m file u can do like below

    // this is the button action method in the custom cell, when the 
    // button tapped, this method is called in this method u are 
    // calling the delegate method which is defined in the controller.
    - (IBAction)buttonAction:(id)sender
     {
        //as in your case user enter the text in textfield and taps button
         if([self.delegate respondsToSelector:@selector(whenDoneButtonClicked:)]) //checking weather it is safe to call the delegate method, it is not need but some times it is necessary to check to avoid crashes
              [self.delegate whenDoneButtonClicked:self]; //pass the cell or if u want text then u can also pass text also by adding 2 or more parameters
     }  
    
    // in the above method u are calling the delegate method by passing
    // the cell (hear the self means -> current object -> nothing but cell)  
    // u are calling the delegate method defined in the controller by
    // passing the  "self" nothing but cell ...  
    

    in maninVc do like below

    in viewcontroller.h file

    #import "CustomCell.h"
    @interface ViewController : UIViewController<UITableViewDataSource,UITableViewDelegate,CellDelegate> //confirms to delegate 
    
    @property (nonatomic, retain) IBOutlet UITableView *tableView;
    //.....  rest of the properties and method 
    
    @end
    

    and in viewcontroller.m file

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
     {
         CustomCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
         if(cell == nil)
         {
            cell = [CustomCell cell]; //initilization if it is nil 
    
         } 
    
        cell.delegate = self; //set the delegate to self 
        //...other code 
        return cell;
     } 
    
    
     //define the delegate method
    
     // when u are calling this method from customCell class by passing 
     // the cell->(self) hear u get the cell        
     - (void)whenDoneButtonClicked:(CustomCell *)cell
     {
        //hear u will get a cell
         NSLog(@"text is:%@",cell.aTextField.text); 
     }
    

    Hope this helps u .. :)

    0 讨论(0)
提交回复
热议问题