【发布时间】:2011-02-05 23:10:38
【问题描述】:
我已经阅读了 Apple 文档并浏览了这些论坛,并且我(成功地)完成了一些教程并让我自己的代表,但我仍然不明白一些东西.我确信这是我要疏远的事情,但是由于我已经尽职尽责,但仍然不知道我做错了什么,我希望你们中的一个人能够告诉我。
情况:
我有一个自定义的 UITableViewCell,里面有一个按钮。当按钮被点击时,我试图从这个自定义单元格的代表中拉出 UIImagePickerController 。我可以捕获并响应按钮点击没有问题,但流程永远不会进入委托方法。换句话说,当我单步执行代码时,一切都很好,直到我尝试从按钮目标方法单步到第二类中的委托方法。它永远不会被调用。
这是代码。
在自定义单元格的接口文件中,我为委托设置了 id,为 UIButton 设置了 IBOutlet,并设置了协议方法:
#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 内部,并尝试在委托中调用委托方法:
#import "CustomPicCell.h"
@implementation CustomPicCell
@synthesize cellDelegate;
...
- (IBAction)getAPic {
NSLog(@"You are here ...");
if (cellDelegate && [cellDelegate respondsToSelector:@selector(getPhoto)]) {
[cellDelegate getPhoto];
}
}
在委托类接口中,我将其设置为自定义单元格的委托:
@interface DelegateClass : UIViewController < ... CustomPicCellDelegate> {
...
在委托类实现中,我尝试拉取 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];
}
我不明白为什么代理永远不会被调用!请理解我确定我错过了一些东西,但我已经多次浏览文档和这些论坛,但我找不到我做错了什么。我在代码的其他部分使用委托就好了。这一点不起作用。谁能告诉我我是什么胖手指? 谢谢
【问题讨论】:
-
你在哪里分配
cellDelegate? -
哦,我可以打自己! Ole Begemann,你是 100% 正确的。我可以发誓我已经设置好了。但我想深夜编码不是我的朋友。我是个白痴,因为我错过了!非常感谢你。那解决了它。我猜总是在早上检查三次。
-
Ole,您能否也分享一下您是如何分配 cellDelegate 的?我的代码与您的代码相似,并且在其他视图控制器中定义的委托方法运行良好时也遇到了类似的问题。但是,在使用 UITableViewCell 的 UITableViewController 中没有调用自定义 UITableViewCell 中定义的委托方法。
-
@Jason 将
cell.cellDelegate=self添加到-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath函数
标签: objective-c delegates uitableview uibutton