addzo addzo - 1 year ago 112
Objective-C Question

Delegate from button tap inside custom uitableviewcell

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;


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

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?

Answer Source

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 
    // ... 
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download