我遇到了一个问题,我将图像添加到具有父级(scrollView)的视图中。问题是,虽然我为每个按钮设置了一个选择器,但只有视图中最近生成的按钮才会响应选择器并调用该方法。
for (int i = 0; i < [_wordsArray count]; i++) {
buttonFrame = CGRectMake(WORDSLEFTBOUNDARY, heightPlacement, [_detailSlider value], [_detailSlider value]);
UIButton *button = [[UIButton alloc] initWithFrame: buttonFrame];
// set the image for the button
[button setImage:[UIImage imageNamed:@"wordicon.png"] forState:UIControlStateNormal];
[button setTag:i]; // set tag identifier for the button
// add selector method 'buttonClickedAction' so that the method is called when a user
// clicks one of the buttons.
[button addTarget:self
action:@selector(buttonClickedAction:)
forControlEvents:UIControlEventTouchUpInside];
[_buttonsView addSubview: button]; // add newly created button as a subview to the buttonView
[_wordButtonArray addObject:button]; // insert newly created button into the wordButtonArray
if (i < [_dateDifferences count]) {
heightPlacement -= 60 + (60 * [[_dateDifferences objectAtIndex:i] intValue]);
}
}
因此,在上面的代码中,我创建了每个按钮并在将其添加到按钮数组之前添加了选择器。但是由于某种原因没有调用我的响应者方法(buttonClickedAction)。