代表不被调用 [英] delegate not being called

查看:130
本文介绍了代表不被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UITableViewCell的子类如下:

  @class MyCell; 

@protocol MyCellDelegate

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

@end

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

- (IBAction)star:(id)sender;
- (IBAction)回复:(id)发件人;
- (IBAction)message:(id)sender;

- (void)showMenu;

@property(nonatomic,retain)id&ConvoreCellDelegate>代表;
@property(nonatomic,retain)NSNumber * mid;
@property(nonatomic,retain)NSNumber * uid;

代理在IBAction中调用:

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

我有另一个UIVewController如下:

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

并在实现我将:

   - (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]){

}
}

然而,这不是内在的。它不会打印DELEGATE被呼叫。为什么是这样?

解决方案

您是否在创建 MyCell



简单的检查方法是添加一个断点,看看代理引用是否为零(0x0)。



当您创建您的单元格时,您必须将代理对象传递给该单元格,以便它有一些要调用的东西。否则,你只是发送一个消息到一个零对象。



所以,假设你正在表视图控制器中正常创建你的单元格:

  MyCell * cell =(MyCell *)[tableView dequeueReusableCellWithIdentifier:@CellID]; 
if(cell == nil){
NSArray * topLevelObjects = [[NSBundle mainBundle] loadNibNamed:@MyCellowner:self options:nil];
cell = [topLevelObjects objectAtIndex:0];
cell.delegate = self; //< --------在创建后设置委托
} else {
NSLog(@cached cell);
}

//你还有其他单元格的东西,例如设置mid和uid。


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?

解决方案

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.

这篇关于代表不被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆