出於某種原因,我的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