【发布时间】:2014-11-21 09:20:10
【问题描述】:
我在 UIScrollView 中有一些 UIButton。首次加载视图时,UIScrollView 中显示了大约 8 个按钮,所有这些按钮都是可见且可点击的。
但是,一旦我滚动,最初加载视图时不存在的任何按钮都不可点击。
鉴于我有一个在视图加载时以编程方式创建每个按钮的函数,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 存在某种问题。这是某种苹果错误吗?我什至尝试在scrollViewDidScroll 函数中添加手势识别器。
-- 编辑-- 另一个有趣的提示,如果我让 UIScrollView 更大,我可以点击更多的按钮,如果我把它变小,我可以点击更少。它肯定与第一个呈现的按钮有关。
也许 iOS 说它正在初始化按钮,但没有将所有 200 多个按钮的选择器保存在内存中。或者每个类具有内置数量的可能选择器/手势识别器。
【问题讨论】:
-
你能分享一些代码吗?至少是您以编程方式创建按钮的部分。
-
请检查您是否在方法后面添加了 (:)
-
是的,我的选择器方法有一个参数,谢谢。
-
尝试更改您的按钮代码。您不使用 alloc/init 创建按钮。而是使用:= [UIButton buttonWithType:]。然后用 setFrame 设置框架。或者使用你现在应该使用的自动布局:-)
-
@CW0007007 我试过这个,但不幸的是它没有用。 :(
标签: ios uiscrollview uibutton