1

实际上,我一直在研究如何从块中调用方法。我正在尝试从 iphone 照片应用程序中获取所有图像。这是代码....

void (^assetGroupEnumerator)(ALAssetsGroup *, BOOL *) = ^(ALAssetsGroup *group, BOOL *stop)   
     if(group != nil) {
        [self.groups addObject:group];
        [group enumerateAssetsUsingBlock:selectionBlock];
    }
};

[library enumerateGroupsWithTypes:groupTypes
                  usingBlock:assetGroupEnumerator
                failureBlock:^(NSError *error) {NSLog(@"A problem occurred");}];

在 selectionBlock ...我只想在用户单击任何图像时添加一个动作。所以我尝试了这段代码..

[imgView addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

buttonPressed 也在同一个 .m 文件中定义。

- (void) buttonPressed: (id) sender {
UIAlertView* av = [[UIAlertView alloc] initWithTitle:@"Howdy!"
                                             message:@"You tapped me."
                                            delegate:nil
                                   cancelButtonTitle:@"Cool"
                                   otherButtonTitles:nil];
[av show];
}

但在 selectionBlock 我得到“没有可见的@interface for 'UIImageView' 声明选择器'addTarget:action:forControlEvents'”

我是 iOS 编程的新手。所以任何帮助都会非常非常感激!

4

2 回答 2

3

-addTarget:action:forControlEvents: is a method on UIControl. UIButton, UISwitch, and so on are all subclasses of UIControl. UIImageView, however, is not, so it does not respond to that selector. Since your imgView variable is a UIImageView, the compiler won't let you call this method on it. This has nothing to do with calling it in from a block, btw - but only with what methods objects declare they respond to.

Hope this helps!

于 2012-08-28T08:24:53.310 回答
0

Change:

[imgView addTarget:self action:@selector(buttonPressed) forControlEvents:UIControlEventTouchUpInside];

to:

[imgView addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];

(with the colon ":" character)

Because buttonPressed has an argument.

于 2012-08-28T08:27:39.957 回答