从自定义uitableviewcell中的按钮开关代表 [英] Delegate from button tap inside custom uitableviewcell

查看:181
本文介绍了从自定义uitableviewcell中的按钮开关代表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经阅读了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目标的方法放在内部,并尝试在委托中调用委托方法:

  #importCustomPicCell.h

@implementation CustomPicCell
@synthesize cellDelegate;

...

- (IBAction)getAPic {
NSLog(@你在这里...);

if(cellDelegate&& [cellDelegate responsesToSelector:@selector(getPhoto)]){
[cellDelegate getPhoto];
}
}

在代理类接口中,将其设置为委托对于自定义单元格:

  @interface DelegateClass:UIViewController< ... CustomPicCellDelegate> {
...

在委托类实现中,我尝试拉UIImagePickerController: / p>

   - (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];
}

我不知道为什么代理人永远不会被调用!请理解我确信我错过了一些东西,但我已经在文档和这些论坛多次,我找不到我做错了什么。我在代码的其他部分使用代码就好了。这一点不行。任何人都可以告诉我我是胖指法吗?
谢谢

解决方案

你为什么这样做?直接在UIViewController类中,您可以为此按钮分配操作。

   - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
[cell 。 picButton addTarget:self
action:@selector(getPhoto)
forControlEvents:UIControlEventTouchUpInside];
// ...
}


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.

The situation:

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.

Here's the code.

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

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

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

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

解决方案

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中的按钮开关代表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆