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
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 //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
@property (weak, nonatomic) IBOutlet UIButton *aButton;
@property (weak, nonatomic) IBOutlet UITextField *aTextField;
@property (nonatomic, assign) id 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 //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 .. :)