将我的自定义单元类中的方法传递给主类

我有一个自定义单元格。 在这里是一个链接到customCell.mUITextField 。 我试图将文本从textField传递给mainVC.m

我在customCell.m有一个公共方法:

 - (NSString *)PassText{ return self.myTextField.text; } 

我如何将该方法传递给我的mainVC.m的string?

你需要做的是:

  1. 定义一个协议
  2. 为您的CustomCell添加此协议的属性
  3. 在你的MainVC实现协议

1.定义一个协议

我们通常把协议的定义放在头文件中(这里是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.为您的CustomCell添加此协议的属性

并将其添加到您的CustomCell @interface CustomCell@end之间。

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

3.在你的MainVC实现协议

在您的MainVC ,执行委托函数如下。

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

以下显示如何使用上述协议。

  1. CustomCell中创buildCustomCell时设置委托。 像下面这样的东西,

     CustomCell *cell = ... allocation and initialization cell.delegate = self; // self is mainVC 
  2. 无论何时您需要在customCell中传递数据NSString,请调用以下命令:

     [self.delegate customCell:self passText:self.myTextField.text]; // self is customCell 

根据我的理解,我认为你正在寻找这种方法,让我知道,如果我在这里是错的。

在索引path中的select行中,首先,您需要find您的自定义单元格,然后find所需的文本字段。 喜欢这个:

  - (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]; } 

现在你可以使用yourTextFieldText来控制mainVC了。

如何创build@class CustomCell;@propety (strong, nonatomic) CustomCell *customCellMainVC @propety (strong, nonatomic) CustomCell *customCell MainVC 。 然后你可以通过self.customCell.textField.text访问它。

迈克,数据传递的正常模式是使用属性和代表。 如果要将信息传递给视图控制器,请使用委托。 我不知道你说的是一个UItableview单元格还是一个收集单元格,当你说一个自定义的单元格。 uitableview和collection视图都有很多方法将信息传递回视图控制器。

这样做有一个更简单的方法。

这是假设你正在以编程方式完成这个工作,并且你的自定义单元格正确设置了textField的工作。

首先,您需要使自定义单元格外部的textField可访问。 你以正常的方式将它作为一个属性放在头文件中:

customCell.h

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

确保您在自定义单元格XIB中分配它。

现在,当我们访问主视图控制器的cellForRowAtIndex中的单元格时,我们有一个指向我们的自定义textField的指针:

 - (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; } 

希望这可以帮助你。

是的,你可以在代表的帮助下做到这一点,

在自定义单元格中,我拿了一个button和文本框,我定义了一个像下面的协议

 //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 

在CustomCell.m文件中,你可以像下面这样做

 // 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 ... 

在maninVc做如下

在viewcontroller.h文件中

 #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 

并在viewcontroller.m文件

 - (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); } 

希望这可以帮助你.. 🙂