0

我将 UIImageView 子类化了,它被称为 ACCascadeImageView。

@interface ACCascadeImageView : UIImageView{
    BOOL isSpotlight;
}

@property (assign, nonatomic) BOOL isSpotlight;

-----

@implementation ACCascadeImageView
@synthesize isSpotlight;

然后我像这样创建实例,并添加一个手势识别器..

ACCascadeImageView *imageview = 
    [[ACCascadeImageView alloc] initWithFrame:imageframe];

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];

[imageview addGestureRecognizer:singleTap];

在 handleSingleTap 方法中,我循环遍历我的 UIScollView 子视图,并尝试为每个子视图执行此操作...

(imageview in this scope is [gestureRecognizer view])
[imageview setIsSpotlight:NO];

但我明白这个...

*** Terminating app due to uncaught exception 'NSInvalidArgumentException', 
reason: '-[UIImageView setIsSpotlight:]: unrecognized selector sent to instance 0x6888be0'

为什么我的ACCascadeImageView突然变成了UIImageView?如果我做了一些愚蠢的事情,我深表歉意,但我之前已经进行了子类UIImageView化。我很困惑。

我应该说我已经NSLog'd [imageview class]得到了,“ ACCascadeImageView”。

这是问题所在:

NSArray *cascadeImages = [PhotoCascade subviews];
for (ACCascadeImageView *v in cascadeImages){
    NSLog(@"RESPONDS: %d", [v respondsToSelector:@selector(setIsSpotlight:)]);
    [v setIsSpotlight:NO];
}

我得到:响应:1 响应:0

然后它就死了。

4

3 回答 3

0

You can't be sure [gestureRecognizer view] is your UIImageView. To check this try NSLog(@"view: %@", [gestureRecognizer.view class]);. My tests says it's just UIView. If you're adding gesture recognizer to your image view the selector will be fired only when the user taps this view. So you could omit those checks at all.

于 2012-09-03T02:37:15.963 回答
0

根据您的结果,并且由于每个对象只测试一次,因此数组中的第一个对象似乎是您的自定义子视图,但第二个对象不是。也许 cascadeImages 中的某个对象不是ACCascadeImageView。遍历数组并对每个数组执行自省,记录结果,以确保数组仅包含 ACCascadeImageViews。

于 2012-09-03T03:54:23.350 回答
0

您是说您handleSingleTap正在返回 TRUE 以响应respondsToSelector:@selector(setIsSpotlight:),但是当您尝试使用 时setIsSpotlight,它会失败吗?那是一个谜。

例如,我创建了这个超级简单的示例,它可以按您的预期工作:

#import "ViewController.h"

@interface Test : UIImageView

@property (assign, nonatomic) BOOL isSpotlight;

@end

@implementation Test

@synthesize isSpotlight;

@end

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    Test *test = [[Test alloc] initWithFrame:self.view.frame];
    [self.view addSubview:test];
    test.userInteractionEnabled = YES;

    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTap:)];
    [test addGestureRecognizer:tap];
}

- (void)handleSingleTap:(UIGestureRecognizer *)sender
{
    Test *imageview = (Test *)[sender view];

    NSLog(@"%s %d", __FUNCTION__, [imageview respondsToSelector:@selector(setIsSpotlight:)]);

    [imageview setIsSpotlight:NO];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end

问题必须很简单,但是由于您的问题中提供的代码片段有限,问题出在哪里并不明显。但是您上面提供的代码不会显示您描述的那种问题。UIImageView您的应用程序中一定有流氓!

于 2012-09-03T03:38:40.833 回答