代表未被调用

发布于 2024-11-05 16:10:58 字数 1599 浏览 0 评论 0原文

我有一个 UITableViewCell 子类,如下所示:

@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;

在 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 IS 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜空下最亮的亮点 2024-11-12 16:10:58

您实际上是在创建 MyCell 后设置委托吗?

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

编辑:根据您下面的评论,很确定您从未设置委托。

创建单元格时,必须将委托对象传递给该单元格,以便它可以调用某些内容。否则,您只是向 nil 对象发送消息。

因此,假设您在表视图控制器中正常创建单元格:

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.

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.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文