delegate not being called

。_饼干妹妹 提交于 2019-12-02 09:21:48

问题


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?


回答1:


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.


来源:https://stackoverflow.com/questions/5940393/delegate-not-being-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!