@selector无法在带有图像的UIButton的imageView中工作

时间:2010-08-09 11:10:25

标签: iphone

我已经为viewcontroller放置了uiscrollview。然后我将我的大尺寸图像设置为imageView in到uiscrollView。然后我将一些按钮设置到imageView中。在那个imageview中,我调用了一个方法“showRestaurant”。但选择器未能调用该方法。

代码:

- (void)handleDoubleTap:(UIGestureRecognizer *)gestureRecognizer {

    float newScale = [imageScrollView zoomScale] * ZOOM_STEP;
    CGRect zoomRect = [self zoomRectForScale:newScale withCenter:[gestureRecognizer locationInView:gestureRecognizer.view]];        

    UIButton *btn1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn1 setImage:[UIImage imageNamed:@"restaurant.png"] forState:UIControlStateNormal];
    UIButton *btn2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn2 setImage:[UIImage imageNamed:@"restaurant.png"] forState:UIControlStateNormal];

    [imageScrollView zoomToRect:zoomRect animated:YES];

    if(newScale==1.500){        
        btn1.frame = CGRectMake(2370.828125,1020.015625,35,34);         
        [btn1 addTarget:self action:@selector(showRestaurant) forControlEvents:UIControlEventTouchUpInside];
        [imageView addSubview:btn1];

        btn2.frame = CGRectMake(2270.828125,1070.015625,35,34);     
        [btn2 addTarget:self action:@selector(showRestaurant) forControlEvents:UIControlEventTouchUpInside];
        [imageView addSubview:btn2];
    }
}

-(void)showRestaurant{
    NSLog(@"testing321");
}

2 个答案:

答案 0 :(得分:2)

程序是否流入“if”?这种比较看起来很奇怪(为什么要将浮点数转换为字符串然后比较字符串)。在“if”中添加一个调试输出,以检查您是否真的在该块中结束。另外,btn1将imageScrollView作为目标,btn2将“self”作为目标。这看起来也不正确。

答案 1 :(得分:1)

请确认您的if子句已进入。检查浮点值的相等性是有问题的,因为可能会舍入到1.500000000001或1.49999999999或类似的东西。

尝试if(newScale>1.49)而不是