Search code examples
iosobjective-cios5ios6

When adding several subviews ( > 4), UIButton stops responding to touches


I am creating a modal view in an App, which contains several subviews. Each one of those subviews has a button and an action related to that button. I use a loop to insert each subview in place. The problem is that the first 4 subviews are ok, from the 5th to the last there are no responses.

Here it is the simplified code related to the problem:

Simplified SubviewView.m

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // More info button
        CGRect buttonFrame = CGRectMake(infoMarginLeft + infoWidthWithInfo, infoHeightControl, 25, 25);
        UIButton * button = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];
        [button setFrame:buttonFrame];
        [button addTarget:self action:@selector(buttonTapped:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    return self;
}

-(void)buttonTapped:(id)sender
{
    NSLog(@"Button tapped!";
}

Simplified view.m

- (void)drawRect:(CGRect)rect
{ 
    for (int i = 0; i < 12; i++) {
        // Color info container
        SubviewView * miniView = [[SubviewView alloc] initWithFrame:CGRectMake(0, 20 * i, 15, 15)];
        miniColorView.backgroundColor = [UIColor clearColor];

        // Adding subview through array to keep track of objects
        [self addSubview:miniColorView];
    }
}

Thanks in advance, I have absolutely no ideia of what's going on :p

--

EDIT

Just found out what it was!

The parent view had as height the screen height. The thing is, some of the subviews were beyond the screen height limit. All I had to do was increase the parent view height =)


Solution

  • I would check to make sure you are not adding your view miniView over top of your button.

    Also this should be moved outside of drawRect, just put this in your init method.

    Edit:

    Set your miniView's background color to another besides clear, and see if you still see your button. If not, then you covered up your button, and it won't receive touch events.