我已经阅读了 Apple 文档,浏览了这些论坛,并且(成功地)完成了一些教程并创建了自己的委托(delegate),但我仍然不明白某些事情.我确信这是我正在间隔的事情,但由于我已经尽职调查但仍然不知道我做错了什么我希望你们中的一个人能够告诉我。
情况:
我有一个自定义的 UITableViewCell,里面有一个按钮。当点击按钮时,我试图从这个自定义单元格的委托(delegate)中拉出 UIImagePickerController。我可以捕捉并响应按钮点击没问题,但流程永远不会进入委托(delegate)方法。换句话说,当我单步执行代码时,一切都很好,直到我尝试从按钮目标方法单步执行到第二类中的委托(delegate)方法。它永远不会被调用。
这是代码。
在自定义单元格的接口(interface)文件中,我为委托(delegate)设置了 id,为 UIButton 设置了 IBOutlet,并设置了协议(protocol)方法:
#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 方法。
在自定义单元格的实现文件中,我将 UIButton 定位的方法放在 touch up 内部,并尝试调用委托(delegate)中的委托(delegate)方法:
#import "CustomPicCell.h"
@implementation CustomPicCell
@synthesize cellDelegate;
...
- (IBAction)getAPic {
NSLog(@"You are here ...");
if (cellDelegate && [cellDelegate respondsToSelector:@selector(getPhoto)]) {
[cellDelegate getPhoto];
}
}
在委托(delegate)类接口(interface)中,我将其设置为自定义单元格的委托(delegate):
@interface DelegateClass : UIViewController < ... CustomPicCellDelegate> {
...
在委托(delegate)类实现中,我尝试提取 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];
}
我不明白为什么代理永远不会被调用!请理解我确信我错过了一些东西,但我已经多次浏览文档和这些论坛,但我找不到我做错了什么。我在代码的其他部分使用委托(delegate)就好了。这一点不起作用。任何人都可以告诉我我在指什么吗? 谢谢
最佳答案
你为什么要这样做?直接在 UIViewController 类中,您可以像这样为按钮分配操作。
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell. picButton addTarget:self
action:@selector(getPhoto)
forControlEvents:UIControlEventTouchUpInside];
// ...
}
关于objective-c - 从自定义 uitableviewcell 中的按钮点击委托(delegate),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4910323/