1

Hi I'm new to delegation, and what I have is a TableView with a custom table view cell which contains the delegate protocol.

And when I click on the button which is subiew of custom view cell it will fire up an event that will pass a value to my ViewControllers method.

TableView is inside the view controller.

The customviewcell is working properly I even log when the button click and it works fine but when it doesn't want to enter the condition called self.delegate respondsToSelector:@selector(btnEditParentLabelText:)

Here is my customviewcell.h

//  ParentTableCell.h


#import <UIKit/UIKit.h>
@protocol ParentTableCellDelegate <NSObject>
@optional
- (void)btnEditParentLabelText:(NSString *)amountParentLabel;
@end

@interface ParentTableCell : UITableViewCell

@property (strong,nonatomic) IBOutlet UITextField *parentLabel;
@property (nonatomic, weak) id <ParentTableCellDelegate>delegate;


-(IBAction)btnEditParentLabel:(id)sender;

@end

customviewcell.m (not all codes just the one needed for delegation)

//  ParentTableCell.m

#import "ParentTableCell.h"

-(IBAction)btnEditParentLabel:(id)sender{
    NSLog(@"click btn");
    if ([self.delegate respondsToSelector:@selector(btnEditParentLabelText:)]) {
        NSLog(@"Inside");
        [self.delegate btnEditParentLabelText:@"test"];
    }

};

@end

Here is my view controller where I implement the TableParentCellDelegate and contains the TableView

//  PositionViewController.h

#import <UIKit/UIKit.h>
#import "ParentTableCell.h"

@interface PositionViewController : UIViewController<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,ParentTableCellDelegate>
{
    UIAlertView *addPostionpopup;
}
#define addPositionAlert 1
#define deletePositionAlert 2
@property(nonatomic,strong) IBOutlet UITableView *positionTable;

- (IBAction)btnAddPosition:(id)sender;

@end

And its m file ill just put the method that is used for the delegate:

- (void)btnEditParentLabelText:(NSString *)amountParentLabel{
    NSLog(@">>> %@", amountParentLabel);
}

Is there something wrong in my implementation?

Thanks

Master Stroke
  • 5,108
  • 2
  • 26
  • 57

2 Answers2

4

Edit the code in PositionViewController.m as below.

- (UITableViewCell *)tableView:(UITableView *)tableView 
                     cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   // Other code to draw the cell
   cell.delegate = self;
   return cell;
}
Alex Cio
  • 6,014
  • 5
  • 44
  • 74
Adarsh V C
  • 2,314
  • 1
  • 20
  • 37
  • Thanks! Can you explain why is this needed? – Andrew James Ramirez Aug 28 '13 at 05:40
  • Delegate is a custom object that should listen the call from your tableviewcell. SO you are supposed to mention the cell that PositionViewController is the delegate to act on button click. – Adarsh V C Aug 28 '13 at 05:42
  • Read this guide. https://developer.apple.com/library/ios/documentation/General/Conceptual/CocoaEncyclopedia/DelegatesandDataSources/DelegatesandDataSources.html#//apple_ref/doc/uid/TP40010810-CH11-SW1 – Adarsh V C Aug 28 '13 at 05:44
0

Do like this


 //  ParentTableCell.h

 #import <UIKit/UIKit.h>
 @protocol ParentTableCellDelegate <NSObject>
 @optional
 - (void)btnEditParentLabelText:(NSString *)amountParentLabel;
 @end

 @interface ParentTableCell : UITableViewCell

 @property (strong,nonatomic) IBOutlet UITextField *parentLabel;
 @property (nonatomic, assign) id <ParentTableCellDelegate>delegate;//made assign for delegates

 -(IBAction)btnEditParentLabel:(id)sender;

 @end

 .m file of ur custom cell
 @synthesize delegate; //synthesize it



 //  PositionViewController.h

 #import <UIKit/UIKit.h>
 #import "ParentTableCell.h"

 @interface PositionViewController :       UIViewController<UITableViewDelegate,UITableViewDataSource,UIAlertViewDelegate,ParentTableCellDelegate>
  {
      UIAlertView *addPostionpopup;
  }
  #define addPositionAlert 1
  #define deletePositionAlert 2
  @property(nonatomic,strong) IBOutlet UITableView *positionTable;

  - (IBAction)btnAddPosition:(id)sender;

 @end


 .m file of your PositionViewController
  - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
 {

  //checkings and all stuffs

  if(cell == nil)
  {
  cell //initilise
  cell. delegate = self; // this is important

  }
  return cell;
 }


Shankar BS
  • 8,394
  • 6
  • 41
  • 53
  • Thanks this also works but the only difference is the delegate. It also works without synthesising it, whats the difference in it? Both of you are correct but I can't choose 2 answer sorry. Cant give rep yet. Thanks again! – Andrew James Ramirez Aug 28 '13 at 05:41
  • May be u noticed a warning that "autosysnthesized property delegate will use sysnthecsized instance variable _delegate...." to get rid of it and one more thing for delegate use assign. for more information this check hear http://stackoverflow.com/questions/7753841/recommended-way-to-declare-delegate-properties-with-arc – Shankar BS Aug 28 '13 at 05:59