本文介绍了从自定义 uitableviewcell 内的按钮点击委托的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了 Apple 文档并浏览了这些论坛,并且我(成功地)完成了一些教程并让我自己的代表,我仍然不明白.我确信这是我正在疏远的事情,但由于我已经尽职尽责,但仍然不知道我做错了什么,我希望你们中的一个人能够告诉我.

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.

情况:

我有一个自定义的 UITableViewCell,里面有一个按钮.当按钮被点击时,我试图从这个自定义单元格的代表中拉出 UIImagePickerController .我可以捕获并响应按钮点击没有问题,但流程永远不会进入委托方法.换句话说,当我单步执行代码时,一切都很好,直到我尝试从按钮目标方法单步到第二类中的委托方法.它永远不会被调用.

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.

这是代码.

在自定义单元格的接口文件中,我为委托设置了 id,为 UIButton 设置了 IBOutlet,并设置了协议方法:

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;

@end

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

UIButton 与 IB 中的 getAPic 方法挂钩.

The UIButton is hooked up to the getAPic method in IB.

在自定义单元格的实现文件中,我将 UIButton 所针对的方法放在里面,并尝试在委托中调用委托方法:

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> {
...

在委托类实现中,我尝试拉取 UIImagePickerController:

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?Thanks

推荐答案

你为什么要这样?直接在 UIViewController 类中,您可以像这样为按钮分配操作.

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
                         action:@selector(getPhoto)
               forControlEvents:UIControlEventTouchUpInside];
    // ...
}

这篇关于从自定义 uitableviewcell 内的按钮点击委托的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 06:55