无法使用viewWithTag传递UIButton图像

时间:2013-09-30 08:44:34

标签: ios objective-c

我不明白为什么我会收到“[UIScrollView imageView]:无法识别的选择器发送到实例”

    for (int i=0;i<[imagesForGame count];i++)
        {      
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(i*310, 0, 310, 200);
            [button setImageWithURL:[NSURL URLWithString:[imagesForGame objectAtIndex:i]] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(openFullPic:) forControlEvents:UIControlEventTouchUpInside];
            [_myScroller addSubview:button];
        }


    -(void)openFullPic:(UIButton *)sender
    {

        UIButton *random = (UIButton *)[_myScroller viewWithTag:sender.tag];
        UIImageView *test = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
        test.image = random.imageView.image; // <-- Error
        ...
    }

3 个答案:

答案 0 :(得分:1)

因为您从未为按钮设置标记。 在第一个循环中,执行button.tag = i;

答案 1 :(得分:1)

UIButton变量中有sender个实例,因此您根本不需要使用viewWithTag; - )

答案 2 :(得分:1)

无论如何发件人都是一个按钮,所以请改用它。

-(void)openFullPic:(UIButton *)sender
{

    UIButton *random = (UIButton *)sender;
    UIImageView *test = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 300, 300)];
    test.image = random.imageView.image; // <-- No more error.
    ...
}