add*_*dzo 8 delegates objective-c uibutton uitableview
我已经阅读了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
Run Code Online (Sandbox Code Playgroud)
UIButton连接到IB中的getAPic方法.
在自定义单元格的实现文件中,我将UIButton目标的方法放在里面,并尝试在委托中调用委托方法:
#import "CustomPicCell.h"
@implementation CustomPicCell
@synthesize cellDelegate;
...
- (IBAction)getAPic {
NSLog(@"You are here ...");
if (cellDelegate && [cellDelegate respondsToSelector:@selector(getPhoto)]) {
[cellDelegate getPhoto];
}
}
Run Code Online (Sandbox Code Playgroud)
在委托类接口中,我将其设置为自定义单元格的委托:
@interface DelegateClass : UIViewController < ... CustomPicCellDelegate> {
...
Run Code Online (Sandbox Code Playgroud)
在委托类实现中,我尝试拉出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];
}
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚为什么代表永远不会被调用!请理解我确定我错过了一些东西,但我已经多次访问了文档和这些论坛,我找不到我做错了什么.我在代码的其他部分使用委托就好了.这一点不起作用.任何人都可以告诉我我的指法是什么吗?谢谢
小智 18
你为什么这样做?直接在UIViewController类中,您可以像这样为按钮分配操作.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell. picButton addTarget:self
action:@selector(getPhoto)
forControlEvents:UIControlEventTouchUpInside];
// ...
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11116 次 |
| 最近记录: |