UIScrollView内有一些UIButtons。第一次加载视图时,UIScrollView中显示的按钮大约有8个,所有这些按钮都是可见和可点击的。UIButtons不能点击UIScrollView编程后添加
但是,一旦我滚动,当最初加载视图时不在那里的任何按钮都不可点击。
鉴于我有一个函数,以编程方式创建每个按钮视图加载,是否有可能addTarget
函数不起作用?我在开始时创建约280个按钮,并以编程方式添加UITouchUpInside事件。
- 编辑 -
这是或多或少的代码,称为viewDidLoad中函数内部
for (int i = 0; i < numberOfButtons; i++){
//Display stuff here
MyButton *aButton = [[MyButton alloc] initWithFrame:CGRectMake(x,y,w,h)];
[aButton.titleLabel setFont:[UIFont fontWithName:@"QuicksandBook-Regular" size: 17.0]];
[aButton setTitle:[currentDisplayArray objectAtIndex:i] forState:UIControlStateNormal];
[aButton addTarget:self
action:@selector(didPressButton:)
forControlEvents:UIControlEventTouchUpInside];
[aButton setUserInteractionEnabled:TRUE];
NSLog(@"width of button = %f height = %f", [aButton frame].size.height, [aButton frame].size.width);
//I printed this to check the height and width were generated correctly.
[btnContainerView addSubview:aButton]; //UIView hooked up to storyboard
[buttonArray addObject:aButton]; //Array to maintain reference to all buttons
}
记住,他们都显示正常,它只是没有在渲染的那些初始框架不会触发“didPressButton”选择器。
- 编辑 -
玩弄后,我认为它的某种与UIScrollView
它是在问题这是某种苹果的bug?我甚至尝试在scrollViewDidScroll
函数中添加手势识别器。
- 编辑 - 另一个有趣的提示,如果我使UIScrollView更大,我可以点击更多的按钮,如果我使它更小,我可以点击更少。它肯定与第一个呈现的按钮有关。
也许iOS说它初始化按钮,但不保存内存中所有200+按钮的选择器。或者每个班级都有内置的可能的选择器/手势识别器。
你能分享一些代码吗?至少是你编程创建按钮的部分。 – 2014-09-26 11:30:44
你去了:) – FaddishWorm 2014-09-26 13:29:03
请检查你是否在你的方法后面加了(:) – 2014-09-26 13:34:36