出于某种原因,我的viewController的addBook方法中初始化的按钮将不响应触摸。我分配给它的选择器从不触发,也不会在点击图像时出现UIControlStateHighlighted图像。iPhone:添加按钮scrollview使按钮无法进入交互
是否有东西在它们到达UIButton之前拦截触摸,或者是它的交互性以某种方式被我对它做的事情所禁用?
- (void)viewDidLoad {
...
_scrollView.contentSize = CGSizeMake(currentPageSize.width, currentPageSize.height);
_scrollView.showsHorizontalScrollIndicator = NO;
_scrollView.showsVerticalScrollIndicator = NO;
_scrollView.scrollsToTop = NO;
_scrollView.pagingEnabled = YES;
...
}
- (void)addBook {
// Make a view to anchor the UIButton
CGRect frame = CGRectMake(0, 0, currentPageSize.width, currentPageSize.height);
UIImageView* bookView = [[UIImageView alloc] initWithFrame:frame];
// Make the button
frame = CGRectMake(100, 50, 184, 157);
UIButton* button = [[UIButton alloc] initWithFrame:frame];
UIImage* bookImage = [UIImage imageNamed:kBookImage0];
// THIS SECTION NOT WORKING!
[button setBackgroundImage:bookImage forState:UIControlStateNormal];
UIImage* bookHighlight = [UIImage imageNamed:kBookImage1];
[button setBackgroundImage:bookHighlight forState:UIControlStateHighlighted];
[button addTarget:self action:@selector(removeBook) forControlEvents:UIControlEventTouchUpInside];
[bookView addSubview:button];
[button release];
[bookView autorelease];
// Add the new view/button combo to the scrollview.
// THIS WORKS VISUALLY, BUT THE BUTTON IS UNRESPONSIVE :(
[_scrollView addSubview:bookView];
}
- (void)removeBook {
NSLog(@"in removeBook");
}
视图层次看起来像这样在Interface Builder:
UIWindow
UINavigationController
RootViewController
UIView
UIScrollView
UIPageControl
想必这样一旦addBook方法运行:
UIWindow
UINavigationController
RootViewController
UIView
UIScrollView
UIView
UIButton
UIPageControl
谢谢,MrMarge。后面的建议奏效了。 – clozach
userInteractionEnabled对于我将UIButton作为子视图添加到的视图而言是NO。这是原因,而不是其他2个设置。 – Alyoshak