为什么的UIButton动作没有被调用时的UIButton是

为什么的UIButton动作没有被调用时的UIButton是

问题描述:

我已创建像含有面板按钮的跳板如下一个UIScrollView内:正确地和卷轴为什么的UIButton动作没有被调用时的UIButton是

-(void)configurePanel{ 

self.criteriaPanel.scrollEnabled=YES; 
self.criteriaPanel=[[UIScrollView alloc] initWithFrame:CGRectMake(
                   0.0f, 
                   0.0f, 
                   [UIScreen mainScreen].bounds.size.width, 
                   (BUTTON_HEIGHT+(3*BUTTON_Y_OFFSET)) 
                   )]; 

UIView *scrollViewContent=[[UIView alloc]init]; 
NSArray *criteriaTypeButtonTitles=[NSArray arrayWithObjects: 
           @"Option1", 
           @"Option2", 
           @"Option3", 
           @"Option4", 
           @"Option5", 
           @"Option6", 
           nil 
           ]; 

for (int i=0; i<[criteriaTypeButtonTitles count]; i++) { 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.tag=i; 
    button.frame = CGRectMake(
          (((BUTTON_X_OFFSET*i)+(BUTTON_WIDTH*i))+BUTTON_X_OFFSET) 
          , BUTTON_Y_OFFSET 
          , BUTTON_WIDTH 
          , BUTTON_HEIGHT 
         ); 
    [button setTitle:[criteriaTypeButtonTitles objectAtIndex:i] forState:UIControlStateNormal]; 
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateHighlighted]; 
    button.titleLabel.font=[UIFont systemFontOfSize:13.0f]; 
    [button setBackgroundColor:[UIColor grayColor]]; 
    [button.layer setCornerRadius:10.0f]; 
    [button.layer setBorderWidth:1.0f]; 
    [button addTarget:self action:@selector(sortByButtonTap:) forControlEvents:UIControlEventTouchDown]; 
    [scrollViewContent addSubview:button]; 
} 

//based upon # of buttons 
self.criteriaPanel.contentSize=CGSizeMake(
             (([criteriaTypeButtonTitles count]*BUTTON_WIDTH)+([criteriaTypeButtonTitles count]*BUTTON_X_OFFSET)), 
             (BUTTON_HEIGHT+(2*BUTTON_Y_OFFSET))); 
[self.criteriaPanel addSubview:scrollViewContent]; 
[self.view addSubview:self.criteriaPanel]; 
} 

的平板显示器,但抽头事件(sortByButtonTap :)因为按钮从来没有被调用过。我怀疑这与包含在滚动视图中的视图中的按钮有关。在阅读了一些其他问题和文档后,我仍然无法弄清楚解决方案应该是什么。

编辑: 我试验了直接将按钮添加到UIScrollView(self.criteriaPanel),按钮轻敲调用sortByButtonTap:因此它是按钮在scrollViewContent中执行的。

+1

任何这方面的运气? – 2012-10-22 14:08:36

+1

这里的任何解决方案? – 2013-07-24 14:47:40

+0

你解决了你的问题吗?请标记正确的答案,将此帖标记为已回答。 – 2015-01-30 12:24:21

您可能需要设置userInteractionEnabled对含有视图scrollViewContent的按钮来接收触摸事件:中

[scrollViewContent setUserInteractionEnabled:YES]; 
+0

不幸的是,这并未导致调用操作方法。我也试了一下,结合demon8733的建议。 – ChrisP 2012-04-24 23:25:03

代替:

[button addTarget:self 
      action:@selector(sortByButtonTap:) 
forControlEvents:UIControlEventTouchDown]; 

尝试:

[button addTarget:self 
      action:@selector(sortByButtonTap:) 
forControlEvents:UIControlEventTouchUpInside]; 
+0

不幸的是,这并没有导致行为方法被调用。我也试了一下,结合gregheo的建议。 – ChrisP 2012-04-24 23:24:42

什么如果您尝试:

[button setExclusiveTouch:YES]; 

当按钮处于也处理触摸事件的视图内时,它们往往会异常工作。

按照此代码查询,

添加UIButton的滚动型

scrollview.pagingEnabled = YES; 
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom]; 
    button.frame = CGRectMake(); 
    [button setTitle:@"X" forState:UIControlStateNormal]; 
    [button setTitleColor:[UIColor clearColor] forState:UIControlStateNormal]; 
    [button addTarget:self action:@selector(btnCloseClicked:withEvent:) forControlEvents:UIControlEventTouchUpInside]; 
    [button addTarget:self action:@selector(btnCloseDraged:withEvent:) forControlEvents:UIControlEventTouchDragInside]; 
    [button addTarget:self action:@selector(btnCloseDragedEnd:withEvent:) forControlEvents:UIControlEventTouchDragExit]; 
    [scrollview addSubview:button]; 
    scrollview.userInteractionEnabled = YES; 

您可以手动把按钮的这三种方法。

让我知道你是否还有疑问。

我认为你的问题只是你设置了错误的UIControlEventUIButton。从UIControlEventTouchDown更改为UIControlEventTouchUpInside。如果连接UIButtonInterface Builder里面,它会永远把它设置为这个UIControlEvent

enter image description here

另一个原因:

否则你UIScrollView控制的相互作用,因为,如果你要滚动它需要知道。如果您将UIButtons放入您的UIScrollView中,则它们将可以访问,但总是会有一点延迟。这就是为什么你也许应该更改设置为UIScrollView

您可以禁用delays content touchesUIButtons会立即访问

enter image description here