8

I have read the Apple docs and I have trolled these forums and I have (successfully) done some tutorials and made my own delegates and I still do not understand something. I am sure that it is something I am spacing off but since I have done due diligence and still don't know what I'm doing wrong I was hoping one of you would be able to tell me.

The situation:

I have a custom UITableViewCell that has a button in it. When the button is tapped I am trying to pull up the UIImagePickerController form a delegate of this custom cell. I can trap and respond to the button tap no problem but the flow never goes to the delegate method. In other words, when I step through the code everything is fine until I try to step from the button target method to the delegate method in the second class. It just never gets called.

Here's the code.

In the interface file for the custom cell I set the id for the delegate, set up the IBOutlet for the UIButton, and set the protocol method:

#import <UIKit/UIKit.h>

@protocol CustomPicCellDelegate;

@interface CustomPicCell : UITableViewCell <UITextInputTraits> {

    UIButton *picButton;
    ...
    id<CustomPicCellDelegate> cellDelegate;
}

@property (nonatomic, retain) IBOutlet UIButton *picButton;
...
@property (nonatomic, assign) id<CustomPicCellDelegate> cellDelegate;
...
- (IBAction)getAPic;

@end

@protocol CustomPicCellDelegate <NSObject> 
- (void)getPhoto;
@end

The UIButton is hooked up to the getAPic method in IB.

In the implementation file for the custom cell I put the method that the UIButton targets on touch up inside and attempt to call the delegate method in the delegate:

#import "CustomPicCell.h"

@implementation CustomPicCell
@synthesize cellDelegate;

...

- (IBAction)getAPic {
    NSLog(@"You are here ...");

    if (cellDelegate && [cellDelegate respondsToSelector:@selector(getPhoto)]) {
        [cellDelegate getPhoto];
    }
}

In the delegate class interface I set it as the delegate for the custom cell:

@interface DelegateClass : UIViewController < ... CustomPicCellDelegate> {
...

In the delegate class implementation I attempt to pull the UIImagePickerController:

- (void)getPhoto {
    UIImagePickerController *picker = [[UIImagePickerController alloc] init];
    picker.delegate = self;

    picker.sourceType = UIImagePickerControllerSourceTypeCamera;

    [self presentModalViewController:picker animated:YES];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo (NSDictionary *)info {
    [picker dismissModalViewControllerAnimated:YES];
}

I can not figure out why the delegate never gets called! Please understand I'm sure that I have missed something but I have been over the docs and these forums many times and I can not find what I am doing wrong. I am using delegates in other parts of the code just fine. This one spot does not work. Can anyone show me what I am fat-fingering? Thanks

addzo
  • 845
  • 3
  • 13
  • 37
  • 4
    Where do you assign `cellDelegate`? – Ole Begemann Feb 05 '11 at 23:41
  • Oh I could smack myself! Ole Begemann you are 100% right. I could have sworn I had set it. but I guess late night coding is not my friend. I'm an idiot for missing that! Thank you very much. That solved it. Always triple check in the morning I guess. – addzo Feb 07 '11 at 21:52
  • Ole, Could you please also share your fix how you assigned the cellDelegate? I have similar code as yours and had a similar problem where the delegate methods defined in other view controllers are working perfectly. However the delegate method defined in a custom UITableViewCell was not being called in the UITableViewController that was using the UITableViewCell. – Jason Sep 22 '11 at 15:20
  • @Jason add `cell.cellDelegate=self` to `-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath` function – Aznix Apr 11 '14 at 06:32

1 Answers1

18

Why are you doing like this? Directly in UIViewController class you can assign action for button like this.

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
     [cell. picButton addTarget:self 
                         action:@selector(getPhoto)
               forControlEvents:UIControlEventTouchUpInside];
    // ... 
}
Lion
  • 872
  • 1
  • 17
  • 43
Prashant Shukla
  • 264
  • 3
  • 5
  • Hi Prashant do you have a link or more information about this approach? – Juan Jan 21 '15 at 12:32
  • 3
    What would be the best way to track the cell of a tapped button? – Islam May 13 '15 at 10:51
  • i am using xib but the button click event fires only for first cell..not for other cells..can any one tell what wrong – saran Jul 29 '16 at 09:02
  • 1
    With this approach you can't find out which cell was tapped without navigating the sender's view hierarchy so this isn't the right approach. Instead use a delegate. – malhal Oct 20 '16 at 19:08