为什么按钮没有调用其他方法?

时间:2015-11-24 10:29:53

标签: ios xcode uibutton

我是iOS新手,这是我的代码

    -(void)getImageV{
    internet *myClass = [[internet alloc]init];
    if ([myClass connectedToInternet]) {
    NSUInteger x =[_relatedOffers count];
    self.scrollViewTest.contentSize = CGSizeMake(x*172, _scrollViewTest.frame.size.height);

    for (int pos=0; pos<[_relatedOffers count]; pos++) {       

        NSLog(@"-------->%d",pos);
        UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        NSLog(@"------>%@",aButton);
        [aButton setTag:pos];
        [aButton setBackgroundColor:[UIColor redColor]];
        [aButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        CGRect buttonFrame = CGRectMake(pos*150, 0, 158, 70);
        buttonFrame.size = CGSizeMake(140, 90);
        aButton.frame = buttonFrame;       
        [self.relatedViewOffer addSubview:aButton];
        NSLog(@"-------->%@",aButton);
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            NSArray *myOrigan = [_relatedOffers objectAtIndex:pos];
            NSURL *aURL = [NSURL URLWithString:[[myOrigan valueForKeyPath:@"offer_image"]stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]];
            UIImage *aImage = [UIImage imageWithData:[NSData dataWithContentsOfURL:aURL]];
            UIImage *image = [MYUtil imageWithImage:aImage scaledToSize:CGSizeMake(158, 70)];
            aImage = nil;
            if(image)
            {
                dispatch_async(dispatch_get_main_queue(), ^{
                [aButton setBackgroundImage:image forState:normal];

                });
            }
        });

    }
    }else{
        [self.view sendSubviewToBack:_rotateView];
        [_rotate stopAnimating];
    }
}

- (void)buttonClicked:(UIButton*)button {
    [self.view bringSubviewToFront:_rotateView];
    [_rotate startAnimating];
    [_relatedViewOffer setHidden:YES];
     _offers =nil;
     _offers = [_relatedOffers objectAtIndex:[button tag]];
    NSArray * allSubviews = [self.relatedViewOffer subviews];
    for(UIView *view in allSubviews)
    {
        if([view isMemberOfClass:[UIButton class]])
        {
            [view removeFromSuperview];
        }
    }    
     [self viewDidLoad];
     [self viewDidAppear:YES];
}

我正在创建水平滚动视图。在这个滚动视图按钮被添加但最多7个按钮它运行良好之后,它不工作? 请解决这个问题。

提前致谢..

1 个答案:

答案 0 :(得分:0)

添加以下内容:self.relatedViewOffer.clipsToBounds = YES;并确定按钮是否仍然可见。如果没有,则按钮超出self.relatedViewOffer的范围。超出其超视图范围的按钮不会接收触摸事件。

如果看不到按钮,则需要使self.relatedViewOffer大到足以适合所有按钮。