【问题标题】:IOS : Call a Method From BlockIOS:从块调用方法
【发布时间】:2012-08-28 08:20:18
【问题描述】:

实际上,我一直在研究如何从块中调用方法。 我正在尝试从 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 中我得到“'UIImageView' 没有可见的@interface 声明选择器'addTarget:action:forControlEvents'”

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

【问题讨论】:

    标签: objective-c ios5 xcode4.3


    【解决方案1】:

    -addTarget:action:forControlEvents:UIControl 上的一个方法。 UIButtonUISwitch 等都是UIControl 的子类。但是,UIImageView 不是,因此它不会响应该选择器。由于您的imgView 变量是UIImageView,编译器不会让您在其上调用此方法。顺便说一句,这与从块中调用它无关 - 但仅与对象声明它们响应的方法有关。

    希望这会有所帮助!

    【讨论】:

    • 那我现在该怎么办?? ...我应该把这个 imgView 放在 UIButton 中,然后添加这个动作吗?我只希望这些 imgView 可点击,以便我可以在 buttonPressed 方法中获取其 url。
    【解决方案2】:

    变化:

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

    到:

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

    (带有冒号“:”字符)

    因为 buttonPressed 有一个参数。

    【讨论】:

    • 是的 -buttonPressed: 有一个参数,但是 UIImageView 仍然没有实现目标动作范式,所以这不是一个答案。
    猜你喜欢
    • 1970-01-01
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 2011-07-02
    • 1970-01-01
    • 1970-01-01
    • 2011-11-19
    • 1970-01-01
    相关资源
    最近更新 更多