Search code examples
iphoneobjective-cipaddelegatesdelegation

delegate not being called


I have subclass a UITableViewCell as follows:

@class MyCell;

@protocol MyCellDelegate

- (void) viewController:(MyCell*)viewCon userId:(NSNumber*)uid andType:(NSString*)type;

@end

@interface MyCell : UITableViewCell <MHLazyTableImageCell>{
    id <MyCellDelegate> delegate;
    NSNumber * mid;
    NSNumber * uid;
}

- (IBAction) star:(id) sender;
- (IBAction) reply:(id) sender;
- (IBAction) message:(id) sender;

- (void) showMenu;

@property (nonatomic, retain) id <ConvoreCellDelegate> delegate;
@property (nonatomic, retain) NSNumber * mid;
@property (nonatomic, retain) NSNumber * uid;

the delegate is called in the IBAction:

- (IBAction) star:(id) sender
{
    [self.delegate viewController:self userId:mid andType:@"star"];  
}

I have another UIVewController as follows:

@interface DetailViewController : UIViewController <UIPopoverControllerDelegate, UITableViewDelegate, UITableViewDataSource, UISplitViewControllerDelegate, RKObjectLoaderDelegate, MHLazyTableImagesDelegate, UITextViewDelegate, ConvoreCellDelegate> {
   .......
}

and in the implementation I put:

- (void) viewController:(MyCell*)viewCon userId:(NSNumber*)uid andType:(NSString*)type
{
    NSLog(@"DELEGATE IS CALLED");
    if ([type isEqualToString:@"star"]){
        NSLog(@"Message id is %@", uid);
    } else if ([type isEqualToString:@"reply"]){
        [message becomeFirstResponder];
        message.text = @"@username";
    } else if ([type isEqualToString:@"message"]){

    }
}

However it is not getting inside this. It never prints DELEGATE IS CALLED. why is this?


Solution

  • Are you actually setting the delegate after creating MyCell?

    Easy way to check is add a breakpoint and see if the delegate reference is nil (0x0).

    EDIT: Base on your comments below, pretty sure you are never setting the delegate.

    When you create your cell, you have to pass in the delegate object to that cell so it has something to call. Otherwise, you are just sending a message to a nil object.

    So, assuming you are creating your cells normally in the table view controller:

    MyCell *cell = (MyCell *)[tableView dequeueReusableCellWithIdentifier:@"CellID"];
        if (cell == nil) {
            NSArray *topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@"MyCell" owner:self options:nil];
            cell = [topLevelObjects objectAtIndex:0];
            cell.delegate = self; // <-------- set the delegate after creation
        } else {
            NSLog(@"cached cell");
        }
    
       //Do you other cell stuff, setting mid and uid for example.