我遇到了一个问题,即我将图像添加到视图中,该视图具有父级(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)由于某种原因没有被调用。
答案 0 :(得分:0)
对于你正在设置相同框架buttonFrame
的所有按钮。所以按钮一个在另一个上面添加,最新的按钮在顶部添加。因此该按钮的操作仅响应
解决方案
的NSLog(@ “%@”,NSStringFromCGRect(buttonFrame));
使用此按钮初始化按钮
UIButton *button=[UIButton buttonWithType:UIButtonTypeCustom];
[button setFrame:buttonFrame];
最后但并非最不重要的是,该行动可以被2个原因阻止
答案 1 :(得分:0)
我不确定但也许是因为你的initWithFrame引用了一个不断更新的CGRect变量。而不是使用变量,直接在initWithFrame
中使用CGRectMakee.g。
UIButton *button = [[UIButton alloc] initWithFrame: CGRectMake(WORDSLEFTBOUNDARY, heightPlacement, [_detailSlider value], [_detailSlider value])];